package org.jeecg.modules.wlhy.shipping.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.enums.ReportPlatformEnum;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.third.ecloud.sdk.util.DateUtil;
import org.jeecg.modules.third.nucarf.NucarfEtcService;
import org.jeecg.modules.third.nucarf.dto.NucarEtcResponderDTO;
import org.jeecg.modules.third.tosapi.dto.TosApiBaseResDTO;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.captain.service.IHyCaptainService;
import org.jeecg.modules.wlhy.company.entity.HyCompanyConfig;
import org.jeecg.modules.wlhy.company.entity.HyCompanyDTO;
import org.jeecg.modules.wlhy.company.service.IHyCompanyConfigService;
import org.jeecg.modules.wlhy.company.service.IHyCompanyService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.fanyun.entity.FyUpDataShippingNoteDTO;
import org.jeecg.modules.wlhy.fanyun.entity.HyFyShipping;
import org.jeecg.modules.wlhy.fanyun.service.IHyFyShippingService;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.mbshipping.entity.MaBangUpDataShippingNoteDTO;
import org.jeecg.modules.wlhy.mbshipping.service.IHyMbShippingService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.report.*;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shiboetc.ShiBoEtcService;
import org.jeecg.modules.wlhy.shiboetc.ShiboEtcReport;
import org.jeecg.modules.wlhy.shiboetc.ShiboEtcReportDTO;
import org.jeecg.modules.wlhy.shiboetc.service.IHyShiboectReportService;
import org.jeecg.modules.wlhy.shipping.constant.ShippingPayStatus;
import org.jeecg.modules.wlhy.shipping.dto.LnTaxShippingNoteDTO;
import org.jeecg.modules.wlhy.shipping.dto.YunFuTongShippingNoteDTO;
import org.jeecg.modules.wlhy.shipping.entity.*;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingParkReportService;
import org.jeecg.modules.wlhy.shipping.service.impl.HyShippingExcelExportImpl;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecg.modules.wlhy.wlxypt.entity.HyShippingNoteHD;
import org.jeecg.modules.wlhy.wlxypt.entity.XlyReportDTO;
import org.jeecg.modules.wlhy.wlxypt.mapper.XlyMapper;
import org.jeecg.modules.wlhy.wlxypt.service.WlxyptServiceApi;
import org.jeecg.modules.wlhy.wlxypt.service.XLYService;
import org.jeecg.modules.wlhy.yunfutong.service.IhyYunfutongServiceAPI;
import org.jeecg.modules.wlhy.yunfutong.service.IhyYunfutongServiceAPI2;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import static java.lang.Thread.sleep;
import static java.util.stream.Collectors.toList;

/**
 * @Description: 上报
 * @Author: jeecg-boot
 * @Date:   2020-12-05
 * @Version: V1.0
 */
@Api(tags="运单表")
@RestController
@RequestMapping("/shipping/hyShippingNote")
//@RequiresRoles(value={"admin", "customer", "driver"},logical = Logical.OR)
@Slf4j
public class HyShippingNoteReportController extends JeecgController<HyShippingNote, IHyShippingNoteService> {

	private static final Logger LOGGER = LoggerFactory.getLogger(HyShippingNoteReportController.class);
	@Resource
	private UserUtil userUtil;
	@Autowired
	private IHyShippingNoteService hyShippingNoteService;

	@Autowired
	private IHyCustomerService hyCustomerService;

	@Autowired
	private IHyCompanyService hyCompanyService;
	@Autowired
	private IHyDriverService hyDriverService;
	@Autowired
	private OrdosReportService ordosReportService;

	@Autowired
	private IHyVehicleService hyVehicleService;
	@Autowired
	private IHyCompanyConfigService companyConfigService;
	@Autowired
	private TjReportService tjReportService;
	@Autowired
	private JgService jgService;

	@Resource
	private IHySettingService settingService;

	@Autowired
	private ShippingNoteEtcService shippingNoteEtcService;

	@Autowired
	private  ShippingNoteDaodaService shippingNoteDaodaService;

	@Autowired
	private ShippingNoteTongdaService shippingNoteTongdaService;

	@Autowired
	private ShippingNoteApiAlct56Service shippingNoteApiAlct56Service;

	@Autowired
	private ShippingNoteTosApiService shippingNoteTosApiService;

	@Autowired
	private IHyMbShippingService mbShippingService ;

	@Autowired
	private IHyFyShippingService fyShippingService;

	@Autowired
	private IhyYunfutongServiceAPI yunfutongServiceAPI;

	@Autowired
	private IhyYunfutongServiceAPI2 yunfutongServiceAPI2;

	@Autowired
	private IHyShippingNoteBillService hyShippingNoteBillService;


	@Autowired
	private IHyOrderService hyOrderService;

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private ShiBoEtcService shiBoEtcService;
	@Autowired
	private IHyShiboectReportService shiboectReportService;

	@Autowired
	private WlxyptServiceApi wlxyptServiceApi;

	@Autowired
	private XLYService xlyService;

	@Autowired
	private NucarfEtcService nucarfEtcService;
	@Resource
	private IHyCustomerConfigService customerConfigService;

	@Autowired
	private IHyCarrierService hyCarrierService;

	@Autowired
	private XlyMapper xlyMapper;


	@Autowired
	private IHyCaptainService hyCaptainService;

	private Lock lock  = new ReentrantLock();



	//region 天津税务数据上报
	@AutoLog(value = "上报税务系统")
	@ApiOperation(value="运单上报税务系统", notes="运单上报税务系统")
	@PutMapping(value = "/taxupload")
	public Result<?> taxupload(@RequestBody HyShippingNote hyShippingNote) throws Exception {

		HyCompanyDTO hyCompany   = hyCompanyService.getCompanyAndConfig(
				 new HyCompanyDTO()
						 .setCompanyId(hyShippingNote.getCompanyId())
						 .setTaxUploadType(1)
						 .setTaxTjUploadStatus("1")
		);
		if(!hyShippingNote.getCompanyId().equals(hyCompany.getId()) ){
			return Result.error("改运单不属于上报税务公司!");
		}

		ApiBaseResDTO reportResult = tjReportService.uploadTax(hyShippingNote);
		Integer code =  StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;
		if( code >= 100 && code < 200 ) {
			return Result.OK("上报成功!");
		}
		return Result.error("上报失败!" + reportResult.getDesc()) ;

	}

	@ApiOperation(value="天津税务数据上报状态信息-分页列表查询", notes="天津税务数据上报状态信息-分页列表查询")
	@GetMapping(value = "/taxupdatalist")
	public Result<?> queryTaxUpdataPageList(TaxUpDataShippingNoteDTO taxUpDataShippingNoteDTO,
											@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<TaxUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
				.eq("tax_upload_type", 1)
				//.eq("tj_tax_upload_status","1")
		);
		if(CollectionUtils.isEmpty(companyConfigs) ){
			return  Result.error("上报公司信息未配置!");
		}
		List<String> companyIds =  companyConfigs.stream().map(
				item -> item.getCompanyId()
		).collect(Collectors.toList());

		//运单
		//queryWrapper.eq("shipping.order_mode", "1");
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");

		if(CollectionUtils.isNotEmpty(companyConfigs) ){
			queryWrapper.in("shipping.company_id", companyIds);
		}else{
			return Result.error("公司信息不匹配");
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",taxUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",taxUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",taxUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getTaxUploadFlag())){
			queryWrapper.eq("shipping.tax_upload_flag",taxUpDataShippingNoteDTO.getTaxUploadFlag());
		}else{
			queryWrapper.eq("shipping.tax_upload_flag","0");
		}

		//司机
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", taxUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", taxUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getVehicleNumber())){

			QueryWrapper<HyVehicle> queryWrapperVehicle =  new QueryWrapper<>();
			queryWrapperVehicle.like("vehicle_number",taxUpDataShippingNoteDTO.getVehicleNumber());
			List <HyVehicle> hyVehicles = hyVehicleService.list(queryWrapperVehicle);
			List<String> vehicleIds =CollectionUtils.isNotEmpty(hyVehicles) ?
					hyVehicles.stream().map(HyVehicle::getId).collect(Collectors.toList()) : new ArrayList <>();

			if(CollectionUtils.isNotEmpty(vehicleIds) ){
				queryWrapper.in("shipping.vehicle_id",vehicleIds);
			}else{
				return  Result.error("车辆信息不匹配");
			}
		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getUploadStatusType())){
			queryWrapper.nested(
					qw-> qw.eq("driver.tj_tax_upload_status","1")
							.eq("company.tj_tax_upload_status","1")
							.eq("shipping.tax_upload_status","1")
							.eq("customer.tj_tax_upload_status","1" )
			);

		}else{

			queryWrapper.nested(
					qw-> qw.ne("driver.tj_tax_upload_status","1")
							.or().ne("company.tj_tax_upload_status","1")
							.or().ne("shipping.tax_upload_status","1")
							.or().ne("customer.tj_tax_upload_status","1" )
			);
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",taxUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}else{

			Date now1 = new Date();
			int month =   now1.getMonth()+1 ;
			if(month == 1){
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(now1);
				calendar.add(Calendar.MONTH, -1);// 把日期往后增加一天.整数往后推,负数往前移动
				// 获取某月最小天数
				int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
				// 设置日历中月份的最小天数
				calendar.set(Calendar.DAY_OF_MONTH, firstDay);

				Date endDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
				String yearStartTime = DateUtils.date2Str(endDate , DateUtils.date_sdf.get()) + " 00:00:00";
				queryWrapper.ge("shipping.pay_dt",yearStartTime);
			}else{
				Long start = System.currentTimeMillis();
				String  yearStartTime =  DateUtils.formatDate1(DateUtils.getYearStartTime(start));
				queryWrapper.ge("shipping.pay_dt",yearStartTime);
			}

		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",taxUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<TaxUpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<TaxUpDataShippingNoteDTO> pageList = tjReportService.getTaxUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}

	@AutoLog(value = "天津税务数据上报")
	@ApiOperation(value="天津税务数据上报", notes="天津税务数据上报")
	@PutMapping(value = "/allTaxUpload")
	public Result<?> allTaxUpload(@RequestBody TaxUpDataShippingNoteDTO taxUpDataShippingNoteDTO) throws Exception {
		//申报公司：河北一六八网络科技有限公司天津第二分公司
		HyCompanyDTO hyCompany   = hyCompanyService.getCompanyAndConfig(
				 new HyCompanyDTO()
						 .setCompanyId(taxUpDataShippingNoteDTO.getCompanyId())
						 .setTaxUploadType(1)
						// .setTaxTjUploadStatus("1")
		);
		if(hyCompany == null){
			return  Result.error("上报公司信息未配置!");
		}
		if(taxUpDataShippingNoteDTO.getCompanyId().equals(hyCompany.getId())){
			if(!"1".equals(taxUpDataShippingNoteDTO.getCompanyTjtaxUploadStatus())) {
				ApiBaseResDTO apiBaseResDTO = hyCompanyService.tjTaxUpload(taxUpDataShippingNoteDTO.getCompanyId());
				LOGGER.error("天津上报公司信息:"+apiBaseResDTO.toString());
			}

			if(!"1".equals(taxUpDataShippingNoteDTO.getCustomerTjtaxUploadStatus())) {
				ApiBaseResDTO apiBaseResDTO = hyCustomerService.tjTaxUpload(
						hyCustomerService.getById(taxUpDataShippingNoteDTO.getCustomerId())
				);
				LOGGER.error("天津上报货主信息:"+apiBaseResDTO.toString());
			}
//			if(!"1".equals(taxUpDataShippingNoteDTO.getDriverTjtaxUploadStatus())) {
			ApiBaseResDTO apiBaseResDTO = hyDriverService.tjTaxUpload(
					hyDriverService.getById(taxUpDataShippingNoteDTO.getDriverId()),
					hyVehicleService.getById(taxUpDataShippingNoteDTO.getVehicleId())
			);
			LOGGER.error("天津上报司机车辆信息:"+apiBaseResDTO.toString());

//			}

			//if(!"1".equals(taxUpDataShippingNoteDTO.getTaxUploadStatus())) {
			ApiBaseResDTO apiBaseResDTO1 = tjReportService.uploadTax(
					new HyShippingNote().setId(taxUpDataShippingNoteDTO.getId())
			);
			LOGGER.error("天津上报运单信息:"+apiBaseResDTO1.toString());

			//}
			return Result.OK("上报成功!") ;
		}

		return Result.error("上报失败，上报税务公司不一致!") ;

	}


	@AutoLog(value = "天津税务数据上报")
	@ApiOperation(value="天津税务数据上报", notes="天津税务数据上报")
	@PutMapping(value = "/allTaxUpload2")
	public Result<?> allTaxUpload2(@RequestBody JSONObject jSONObject) throws Exception {


		String ids = jSONObject.getString("ids");

		TaxUpDataShippingNoteDTO taxUpDataShippingNoteDTO =new TaxUpDataShippingNoteDTO();
		taxUpDataShippingNoteDTO.setUploadStatusType("1");
		QueryWrapper<TaxUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		queryWrapper.in("shipping.id", Arrays.asList(ids.split(",")));

		List<HyCompanyConfig> companyConfigs = companyConfigService.list(new QueryWrapper<HyCompanyConfig>()
						.eq("tax_upload_type", 1)
				//.eq("tj_tax_upload_status","1")
		);
		if(CollectionUtils.isEmpty(companyConfigs) ){
			return  Result.error("上报公司信息未配置!");
		}
		List<String> companyIds =  companyConfigs.stream().map(
				item -> item.getCompanyId()
		).collect(Collectors.toList());

		//运单
		//queryWrapper.eq("shipping.order_mode", "1");
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");

		if(CollectionUtils.isNotEmpty(companyConfigs) ){
			queryWrapper.in("shipping.company_id", companyIds);
		}else{
			return Result.error("公司信息不匹配");
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",taxUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",taxUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",taxUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getTaxUploadFlag())){
			queryWrapper.eq("shipping.tax_upload_flag",taxUpDataShippingNoteDTO.getTaxUploadFlag());
		}else{
			queryWrapper.eq("shipping.tax_upload_flag","0");
		}

		//司机
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", taxUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", taxUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getVehicleNumber())){

			QueryWrapper<HyVehicle> queryWrapperVehicle =  new QueryWrapper<>();
			queryWrapperVehicle.like("vehicle_number",taxUpDataShippingNoteDTO.getVehicleNumber());
			List <HyVehicle> hyVehicles = hyVehicleService.list(queryWrapperVehicle);
			List<String> vehicleIds =CollectionUtils.isNotEmpty(hyVehicles) ?
					hyVehicles.stream().map(HyVehicle::getId).collect(Collectors.toList()) : new ArrayList <>();

			if(CollectionUtils.isNotEmpty(vehicleIds) ){
				queryWrapper.in("shipping.vehicle_id",vehicleIds);
			}else{
				return  Result.error("车辆信息不匹配");
			}
		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getUploadStatusType())){
			queryWrapper.nested(
					qw-> qw.eq("driver.tj_tax_upload_status","1")
							.eq("company.tj_tax_upload_status","1")
							.eq("shipping.tax_upload_status","1")
							.eq("customer.tj_tax_upload_status","1" )
			);

		}else{

			queryWrapper.nested(
					qw-> qw.ne("driver.tj_tax_upload_status","1")
							.or().ne("company.tj_tax_upload_status","1")
							.or().ne("shipping.tax_upload_status","1")
							.or().ne("customer.tj_tax_upload_status","1" )
			);
		}

		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",taxUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}else{

			Date now1 = new Date();
			int month =   now1.getMonth()+1 ;
			if(month == 1){
				Calendar calendar = new GregorianCalendar();
				calendar.setTime(now1);
				calendar.add(Calendar.MONTH, -1);// 把日期往后增加一天.整数往后推,负数往前移动
				// 获取某月最小天数
				int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
				// 设置日历中月份的最小天数
				calendar.set(Calendar.DAY_OF_MONTH, firstDay);

				Date endDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
				String yearStartTime = DateUtils.date2Str(endDate , DateUtils.date_sdf.get()) + " 00:00:00";
				queryWrapper.ge("shipping.pay_dt",yearStartTime);
			}else{
				Long start = System.currentTimeMillis();
				String  yearStartTime =  DateUtils.formatDate1(DateUtils.getYearStartTime(start));
				queryWrapper.ge("shipping.pay_dt",yearStartTime);
			}

		}
		if(StringUtils.isNotEmpty(taxUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",taxUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");

		Page<TaxUpDataShippingNoteDTO> page = new Page<>(1, 10000);
		IPage<TaxUpDataShippingNoteDTO> pageList = tjReportService.getTaxUpDataPageList(page, queryWrapper);

		for (TaxUpDataShippingNoteDTO record : pageList.getRecords()) {
			HyCompanyDTO hyCompany   = hyCompanyService.getCompanyAndConfig(
					new HyCompanyDTO()
							.setCompanyId(record.getCompanyId())
							.setTaxUploadType(1)
					// .setTaxTjUploadStatus("1")
			);
			if(hyCompany == null){
				return  Result.error("上报公司信息未配置!");
			}
			if(record.getCompanyId().equals(hyCompany.getId())){
				if(!"1".equals(record.getCompanyTjtaxUploadStatus())) {
					ApiBaseResDTO apiBaseResDTO = hyCompanyService.tjTaxUpload(record.getCompanyId());
					LOGGER.error("天津上报公司信息:"+apiBaseResDTO.toString());
				}

				if(!"1".equals(record.getCustomerTjtaxUploadStatus())) {
					ApiBaseResDTO apiBaseResDTO = hyCustomerService.tjTaxUpload(
							hyCustomerService.getById(record.getCustomerId())
					);
					LOGGER.error("天津上报货主信息:"+apiBaseResDTO.toString());
				}
//			if(!"1".equals(taxUpDataShippingNoteDTO.getDriverTjtaxUploadStatus())) {
				ApiBaseResDTO apiBaseResDTO = hyDriverService.tjTaxUpload(
						hyDriverService.getById(record.getDriverId()),
						hyVehicleService.getById(record.getVehicleId())
				);
				LOGGER.error("天津上报司机车辆信息:"+apiBaseResDTO.toString());

//			}

				//if(!"1".equals(taxUpDataShippingNoteDTO.getTaxUploadStatus())) {
				ApiBaseResDTO apiBaseResDTO1 = tjReportService.uploadTax(
						new HyShippingNote().setId(record.getId())
				);
				LOGGER.error("天津上报运单信息:"+apiBaseResDTO1.toString());

				//}
//				return Result.OK("上报成功!") ;
			}
		}


		return Result.OK("上报成功") ;

	}
	@AutoLog(value = "天津税务取消上报")
	@ApiOperation(value="天津税务取消上报", notes="天津税务取消上报")
	@PostMapping(value = "/cancelReport")
	public Result<?> cancelReport(@RequestBody HyShippingNote hyShippingNote) {


		HyShippingNote hyShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
		if(hyShippingNoteDb == null){
			return Result.error("信息不存在!") ;
		}
		hyShippingNoteDb.setTaxUploadFlag("1");

		return  hyShippingNoteService.updateById(hyShippingNoteDb)  ? Result.OK("取消上报成功!") : Result.error("取消上报失败!");

	}

	@AutoLog(value = "天津税务恢复上报")
	@ApiOperation(value="天津税务恢复上报", notes="天津税务恢复上报")
	@PostMapping(value = "/resumptionReport")
	public Result<?> resumptionReport(@RequestBody HyShippingNote hyShippingNote) {

		HyShippingNote hyShippingNoteDb = hyShippingNoteService.getById(hyShippingNote.getId());
		if(hyShippingNoteDb == null){
			return Result.error("信息不存在!") ;
		}
		hyShippingNoteDb.setTaxUploadFlag("0");
		return  hyShippingNoteService.updateById(hyShippingNoteDb)  ? Result.OK("恢复上报成功!") : Result.error("恢复上报失败!");

	}


	//endregion

	//region 监管平台数据上报


	@RequestMapping(value = "/upExportXls")
	public ModelAndView exportXls(HttpServletRequest request, UpDataShippingNoteDTO2 shippingNoteDTO) {

		QueryWrapper<HyCompanyConfig> queryCompanyWrapper = new QueryWrapper();
		queryCompanyWrapper.ne("report_platform_type",0);
		if(StringUtils.isNotBlank(shippingNoteDTO.getCompanyId())){
			queryCompanyWrapper.eq("company_id",shippingNoteDTO.getCompanyId());
		}
		queryCompanyWrapper.orderByAsc("report_platform_type");
		List<HyCompanyConfig> companyConfigs   = companyConfigService.list(queryCompanyWrapper);
		if(CollectionUtils.isEmpty(companyConfigs)){
			throw new JeecgBootException("上报公司配置信息不存在!");
		}

		HyCompanyConfig companyConfig = companyConfigs.get(0);

		QueryWrapper<UpDataShippingNoteDTO2> queryWrapper = new QueryWrapper();
		String shippingPayStatus = shippingNoteDTO.getShippingPayStatus();

		if(userUtil.isSys()){
			if(StringUtils.isNotEmpty(shippingNoteDTO.getCustomerId())){
				queryWrapper.eq("shipping.customer_id",shippingNoteDTO.getCustomerId());
			}
		}else {
			HyCustomer customer = userUtil.getCustomer();
			if(customer==null){
				throw new JeecgBootException("货主信息不存在!");
			}
			if("4".equals(shippingPayStatus) || "7".equals(shippingPayStatus) ){
				queryWrapper.eq("shipping.customer_id", customer.getId());
			}else {
				queryWrapper.and(
						qw -> qw.eq("shipping.customer_id", customer.getId())
								.or()
								.eq("shipping.agent_id", customer.getId()));
			}
		}
		queryWrapper.eq("shipping.del_flag", "0");
		setQueryWrapper2(shippingNoteDTO, queryWrapper,companyConfig);
		String invoiceId = shippingNoteDTO.getInvoiceId();
		if(StringUtils.isNotEmpty(invoiceId)){
			queryWrapper.eq("shipping.invoice_id",invoiceId);
		}


		//上报时间
		//queryWrapper.eq("shipping.shipping_pay_status",shippingPayStatus);

		String byDesc = shippingNoteDTO.getByDesc();
		if (StringUtils.isNotEmpty(byDesc)) {
			String[] bys = byDesc.split(",");
			for (String by : bys) {
				queryWrapper.orderByDesc("shipping." + by);
			}

		} else {
			queryWrapper.orderByDesc("shipping.pay_dt");
		}

		String selections = request.getParameter("selections");
		if(StringUtils.isNotEmpty(selections)){
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("shipping.id", selectionList);
		}

		String domianURL = request.getParameter("domianURL");
		if(StringUtils.isNotEmpty(domianURL)) {
			domianURL += "/";
		}else {
			domianURL = "";
		}

		List<UpDataShippingNoteDTO2> list = jgService.getList1(queryWrapper);
		for (UpDataShippingNoteDTO2 hyShippingNoteDTO : list) {
			hyShippingNoteDTO.setCostPayTotal(hyShippingNoteDTO.getCustomerFee());

			String shippingContract = hyShippingNoteDTO.getShippingContract();
			if(StringUtils.isNotEmpty(shippingContract)){
				hyShippingNoteDTO.setShippingContract(domianURL  + shippingContract);
			}
			String imgLoading = hyShippingNoteDTO.getImgLoading();
			if(StringUtils.isNotEmpty(imgLoading)){
				hyShippingNoteDTO.setImgLoading(domianURL  + imgLoading);
			}

			String imgUnloading = hyShippingNoteDTO.getImgUnloading();
			if(StringUtils.isNotEmpty(imgUnloading)){
				hyShippingNoteDTO.setImgUnloading(domianURL  + imgUnloading);
			}
		}

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String title="运单上报导出表";
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, UpDataShippingNoteDTO2.class);

		//按选择列导出
		String selCols = request.getParameter("selCols");
		if("1".equals(selCols)) {
			String field = request.getParameter("field");
			if (StringUtils.isNotEmpty(field)) {
				List<String> fields = Arrays.asList(field.split(","));
				List<String> selectCols = new ArrayList<>();
				for (String s : fields) {
					if (StringUtils.isNotEmpty(s)) {
						int idx = s.indexOf("_dictText");
						if (idx > -1) {
							s = s.substring(0, idx);
						}
						selectCols.add(s);
					}
				}
				mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(selectCols, ","));
			}
		}
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}



	private void setQueryWrapper(UpDataShippingNoteDTO upDataShippingNoteDTO,
								 QueryWrapper<UpDataShippingNoteDTO> queryWrapper ,
								 HyCompanyConfig companyConfig){
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");

		//支付状态查询
		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingPayStatus())) {
			queryWrapper.eq("shipping.shipping_pay_status", upDataShippingNoteDTO.getShippingPayStatus());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
		}else{
			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);
		//资金
//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillNum())){
//			queryWrapper.eq("bill.bill_num",upDataShippingNoteDTO.getBillNum() );
//		}
//		queryWrapper.eq("bill.bill_type","1");

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getUploadStatusType())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
//							qw-> qw.eq("driver_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("driver_report.driver_upload_status","1")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
//							qw-> qw.eq("vehicle_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("vehicle_report.vehicle_upload_status","1")
					).nested(
							qw-> qw.eq("shipping.upload_status", 1)
							//qw-> qw.eq("shipping.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("shipping.upload_status","1")
					).nested(
							//qw-> qw.eq("bill.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("bill.upload_status","1")
							qw-> qw.eq("bill.upload_status", 1)
					)
			);


		}else{//待上报 上报失败

			queryWrapper.nested(
				qw1-> qw1.nested(
						qw-> qw.ne("driver_report.driver_upload_status","1")
								.or().isNull("driver_report.driver_upload_status")
				).or().nested(
						qw-> qw.ne("vehicle_report.vehicle_upload_status","1")
								.or().isNull("vehicle_report.vehicle_upload_status")
				).or().nested(
						qw-> qw.isNull("shipping.upload_status")
								.or().ne("shipping.upload_status","1")
				).or().nested(
						qw-> qw.isNull("bill.upload_status")
								.or().ne("bill.upload_status","1")
				)
			);

		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		return;
	}
	private void setQueryWrapper1(UpDataShippingNoteDTO1 upDataShippingNoteDTO,
								 QueryWrapper<UpDataShippingNoteDTO> queryWrapper ,
								 HyCompanyConfig companyConfig){
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");

		//支付状态查询
		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingPayStatus())) {
			queryWrapper.eq("shipping.shipping_pay_status", upDataShippingNoteDTO.getShippingPayStatus());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
		}else{
			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);
		//资金
//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillNum())){
//			queryWrapper.eq("bill.bill_num",upDataShippingNoteDTO.getBillNum() );
//		}
//		queryWrapper.eq("bill.bill_type","1");

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getUploadStatusType())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
//							qw-> qw.eq("driver_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("driver_report.driver_upload_status","1")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
//							qw-> qw.eq("vehicle_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("vehicle_report.vehicle_upload_status","1")
					).nested(
							qw-> qw.eq("shipping.upload_status", 1)
							//qw-> qw.eq("shipping.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("shipping.upload_status","1")
					).nested(
							//qw-> qw.eq("bill.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("bill.upload_status","1")
							qw-> qw.eq("bill.upload_status", 1)
					)
			);


		}else{//待上报 上报失败

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.ne("driver_report.driver_upload_status","1")
									.or().isNull("driver_report.driver_upload_status")
					).or().nested(
							qw-> qw.ne("vehicle_report.vehicle_upload_status","1")
									.or().isNull("vehicle_report.vehicle_upload_status")
					).or().nested(
							qw-> qw.isNull("shipping.upload_status")
									.or().ne("shipping.upload_status","1")
					).or().nested(
							qw-> qw.isNull("bill.upload_status")
									.or().ne("bill.upload_status","1")
					)
			);

		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillUploadDate_begen())){
			queryWrapper.ge("shipping.upload_date",upDataShippingNoteDTO.getBillUploadDate_begen()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillUploadDate_end() )){
			queryWrapper.le("shipping.upload_date",upDataShippingNoteDTO.getBillUploadDate_end()+" 23:59:59" );
		}

		return;
	}
	private void setQueryWrapper2(UpDataShippingNoteDTO2 upDataShippingNoteDTO,
								  QueryWrapper<UpDataShippingNoteDTO2> queryWrapper ,
								  HyCompanyConfig companyConfig){
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");

		//支付状态查询
		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingPayStatus())) {
			queryWrapper.eq("shipping.shipping_pay_status", upDataShippingNoteDTO.getShippingPayStatus());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
		}else{
			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);
		//资金
//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillNum())){
//			queryWrapper.eq("bill.bill_num",upDataShippingNoteDTO.getBillNum() );
//		}
//		queryWrapper.eq("bill.bill_type","1");

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getUploadStatusType())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
//							qw-> qw.eq("driver_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("driver_report.driver_upload_status","1")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
//							qw-> qw.eq("vehicle_report.report_platform_type",companyConfig.getReportPlatformType())
//									.eq("vehicle_report.vehicle_upload_status","1")
					).nested(
							qw-> qw.eq("shipping.upload_status", 1)
							//qw-> qw.eq("shipping.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("shipping.upload_status","1")
					).nested(
							//qw-> qw.eq("bill.report_platform_type", companyConfig.getReportPlatformType())
							//		.eq("bill.upload_status","1")
							qw-> qw.eq("bill.upload_status", 1)
					)
			);


		}else{//待上报 上报失败

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.ne("driver_report.driver_upload_status","1")
									.or().isNull("driver_report.driver_upload_status")
					).or().nested(
							qw-> qw.ne("vehicle_report.vehicle_upload_status","1")
									.or().isNull("vehicle_report.vehicle_upload_status")
					).or().nested(
							qw-> qw.isNull("shipping.upload_status")
									.or().ne("shipping.upload_status","1")
					).or().nested(
							qw-> qw.isNull("bill.upload_status")
									.or().ne("bill.upload_status","1")
					)
			);

		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillUploadDate_begen())){
			queryWrapper.ge("shipping.upload_date",upDataShippingNoteDTO.getBillUploadDate_begen()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillUploadDate_end() )){
			queryWrapper.le("shipping.upload_date",upDataShippingNoteDTO.getBillUploadDate_end()+" 23:59:59" );
		}

		return;
	}



	private void setHuaibeiQueryWrapper(UpDataShippingNoteDTO upDataShippingNoteDTO,
									 QueryWrapper<UpDataShippingNoteDTO> queryWrapper){
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");

		//Delete at 2023-06-09 for 运单签收完成就可以显示在待上报界面，在资金流水上报时判断支付状态
		//queryWrapper.eq("shipping.shipping_pay_status", "6");

//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
//			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
//		}else{
//			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
//		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getParkUploadFlag())
				&& "1".equals(upDataShippingNoteDTO.getParkUploadFlag())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
									.eq("driver_report.report_platform_type","10")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
									.eq("vehicle_report.report_platform_type","10")
					).nested(
							qw-> qw.eq("shipping.tongda_upload_status","1")
					).nested(
							qw-> qw.eq("shipping.daoda_upload_status","1")
					)
			);

		} else {//待上报&上报失败

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.ne("driver_report.driver_upload_status","1")
									.or().isNull("driver_report.driver_upload_status")
					).or().nested(
							qw-> qw.ne("vehicle_report.vehicle_upload_status","1")
									.or().isNull("vehicle_report.vehicle_upload_status")
					).or().nested(
							qw-> qw.isNull("shipping.tongda_upload_status")
									.or().ne("shipping.tongda_upload_status","1")
					).or().nested(
							qw-> qw.isNull("shipping.daoda_upload_status")
									.or().ne("shipping.daoda_upload_status","1")
					)
			);
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}

		return;
	}


	private void setOrdosQueryWrapper(UpDataShippingNoteDTO upDataShippingNoteDTO,
										QueryWrapper<UpDataShippingNoteDTO> queryWrapper){
		//运单
		queryWrapper.in("shipping.shipping_status", "3","4","5");

		//Delete at 2023-06-09 for 运单签收完成就可以显示在待上报界面，在资金流水上报时判断支付状态
		//queryWrapper.eq("shipping.shipping_pay_status", "6");

//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
//			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
//		}else{
//			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
//		}

		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getCreateTime_begin())) {
			queryWrapper.ge("shipping.create_time", upDataShippingNoteDTO.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getCreateTime_end())) {
			queryWrapper.le("shipping.create_time", upDataShippingNoteDTO.getCreateTime_end() + " 23:59:59");
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getParkUploadFlag())
				&& "1".equals(upDataShippingNoteDTO.getParkUploadFlag())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
									.eq("driver_report.report_platform_type","200")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
									.eq("vehicle_report.report_platform_type","200")
					).nested(
							qw-> qw.eq("ordosreport.load_report_status","1")
					).nested(
							qw-> qw.eq("ordosreport.unload_report_status","1")
					).nested(
							qw-> qw.eq("ordosreport.trace_report_status","1")
					).nested(
							qw-> qw.eq("ordosreport.shipping_report_status","1")
					).nested(
							qw-> qw.eq("ordosreport.pay_report_status","1")
					).nested(
							qw-> qw.eq("ordosreport.invonce_report_status","1")
					)
			);

		} else {//待上报&上报失败

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.report_platform_type","200")
									.ne("driver_report.driver_upload_status","1")
									.or().isNull("driver_report.driver_upload_status")
					).or().nested(
							qw-> qw.eq("vehicle_report.report_platform_type","200")
									.ne("vehicle_report.vehicle_upload_status","1")
									.or().isNull("vehicle_report.vehicle_upload_status")
					).or().nested(
							qw-> qw.isNull("ordosreport.load_report_status")
									.or().ne("ordosreport.load_report_status","1")
					).or().nested(
							qw-> qw.isNull("ordosreport.unload_report_status")
									.or().ne("ordosreport.unload_report_status","1")
					).or().nested(
							qw-> qw.isNull("ordosreport.trace_report_status")
									.or().ne("ordosreport.trace_report_status","1")
					).or().nested(
							qw-> qw.isNull("ordosreport.shipping_report_status")
									.or().ne("ordosreport.shipping_report_status","1")
					).or().nested(
							qw-> qw.isNull("ordosreport.pay_report_status")
									.or().ne("ordosreport.pay_report_status","1")
					).or().nested(
							qw-> qw.isNull("ordosreport.invonce_report_status")
									.or().ne("ordosreport.invonce_report_status","1")
					)
			);
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}

		return;
	}

	private void setParkQueryWrapper(UpDataShippingNoteDTO upDataShippingNoteDTO,
								 QueryWrapper<UpDataShippingNoteDTO> queryWrapper ,
								 HyCompanyConfig companyConfig){
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");

		//Delete at 2023-06-09 for 运单签收完成就可以显示在待上报界面，在资金流水上报时判断支付状态
		//queryWrapper.eq("shipping.shipping_pay_status", "6");

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCompanyId())){
			queryWrapper.eq("shipping.company_id",upDataShippingNoteDTO.getCompanyId());
		}else{
			queryWrapper.eq("shipping.company_id",companyConfig.getCompanyId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",upDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",upDataShippingNoteDTO.getShippingNoteNumber());
		}

		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getCreateTime_begin())) {
			queryWrapper.ge("shipping.create_time", upDataShippingNoteDTO.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(upDataShippingNoteDTO.getCreateTime_end())) {
			queryWrapper.le("shipping.create_time", upDataShippingNoteDTO.getCreateTime_end() + " 23:59:59");
		}

		//司机
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", upDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", upDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", upDataShippingNoteDTO.getVehicleNumber());
		}

		//非删除运单
		queryWrapper.eq("shipping.del_flag", 0);
		//Modified at 2023-08-29 for 切中交、阿帕系统导入的运单会标记2，不能上报
		queryWrapper.ne("shipping.import_flag", 2);
		//资金
//		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getBillNum())){
//			queryWrapper.eq("bill.bill_num",upDataShippingNoteDTO.getBillNum() );
//		}
//		queryWrapper.eq("bill.bill_type","1");

		//已上报
		if( StringUtils.isNotEmpty(upDataShippingNoteDTO.getParkUploadFlag())
		 && "1".equals(upDataShippingNoteDTO.getParkUploadFlag())){

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.eq("driver_report.driver_upload_status","1")
					).nested(
							qw-> qw.eq("vehicle_report.vehicle_upload_status","1")
					).nested(
							qw-> qw.eq("parkreport.report_platform_type", companyConfig.getParkReportPlatformType())
									.eq("parkreport.upload_status","1")
					).nested(
							qw-> qw.eq("parkreport.report_platform_type", companyConfig.getParkReportPlatformType())
									.eq("parkreport.zj_upload_status","1")
					)
			);

		} else {//待上报&上报失败

			queryWrapper.nested(
					qw1-> qw1.nested(
							qw-> qw.ne("driver_report.driver_upload_status","1")
									.or().isNull("driver_report.driver_upload_status")
					).or().nested(
							qw-> qw.ne("vehicle_report.vehicle_upload_status","1")
									.or().isNull("vehicle_report.vehicle_upload_status")
					).or().nested(
							qw-> qw.isNull("parkreport.upload_status")
									.or().ne("parkreport.upload_status","1")
					).or().nested(
							qw-> qw.isNull("parkreport.zj_upload_status")
									.or().ne("parkreport.zj_upload_status","1")
					)
			);
		}

		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",upDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}

		return;
	}


	@ApiOperation(value="监管平台数据上报状态信息-分页列表查询", notes="监管平台数据上报状态信息-分页列表查询")
	@GetMapping(value = "/updatalist")
	public Result<?> queryUpdataPageList(UpDataShippingNoteDTO1 upDataShippingNoteDTO,
										 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyCompanyConfig> queryCompanyWrapper = new QueryWrapper();
		queryCompanyWrapper.ne("report_platform_type",0);
		if(StringUtils.isNotBlank(upDataShippingNoteDTO.getCompanyId())){
			queryCompanyWrapper.eq("company_id",upDataShippingNoteDTO.getCompanyId());
		}
		queryCompanyWrapper.orderByAsc("report_platform_type");
		List<HyCompanyConfig> companyConfigs = companyConfigService.list(queryCompanyWrapper);
		if (CollectionUtils.isEmpty(companyConfigs)) {
			log.error("上报公司配置信息不存在");
			return Result.error("上报公司配置信息不存在!");
		}

		HyCompanyConfig companyConfig = companyConfigs.get(0);
		QueryWrapper<UpDataShippingNoteDTO> queryWrapper = new QueryWrapper();
		String shippingPayStatus = upDataShippingNoteDTO.getShippingPayStatus();

		if (userUtil.isSys()) {
			if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCustomerId())){
				queryWrapper.eq("shipping.customer_id",upDataShippingNoteDTO.getCustomerId());
			}
		} else {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			if ("4".equals(shippingPayStatus) || "7".equals(shippingPayStatus) ) {
				queryWrapper.eq("shipping.customer_id", customer.getId());
			} else {
				queryWrapper.and(
						qw -> qw.eq("shipping.customer_id", customer.getId())
								.or()
								.eq("shipping.agent_id", customer.getId()));
			}
		}

		setQueryWrapper1(upDataShippingNoteDTO, queryWrapper, companyConfig);

		queryWrapper.groupBy("shipping.shipping_note_number");
		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<UpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<UpDataShippingNoteDTO> pageList = jgService.getUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="园区数据上报信息-分页列表查询", notes="园区数据上报信息-分页列表查询")
	@GetMapping(value = "/parkupdatalist")
	public Result<?> queryParkUpdataPageList(UpDataShippingNoteDTO upDataShippingNoteDTO,
										 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyCompanyConfig> queryCompanyWrapper = new QueryWrapper();
		queryCompanyWrapper.ne("park_report_platform_type",0);
		if(StringUtils.isNotBlank(upDataShippingNoteDTO.getCompanyId())){
			queryCompanyWrapper.eq("company_id", upDataShippingNoteDTO.getCompanyId());
		}
		queryCompanyWrapper.orderByAsc("park_report_platform_type");
		List<HyCompanyConfig> companyConfigs = companyConfigService.list(queryCompanyWrapper);
		if(CollectionUtils.isEmpty(companyConfigs)){
			return Result.error("园区上报公司配置信息不存在!");
		}

		HyCompanyConfig companyConfig = companyConfigs.get(0);
		QueryWrapper<UpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		if (userUtil.isSys()) {
			if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCustomerId())){
				queryWrapper.eq("shipping.customer_id",upDataShippingNoteDTO.getCustomerId());
			}
		} else {
			return Result.error("当前登录角色无园区上报权限");
		}

		setParkQueryWrapper(upDataShippingNoteDTO, queryWrapper, companyConfig);

		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<UpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<UpDataShippingNoteDTO> pageList = jgService.getParkUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="淮北数据上报信息-分页列表查询", notes="淮北数据上报信息-分页列表查询")
	@GetMapping(value = "/huaibeiupdatalist")
	public Result<?> queryHuaibeiUpdatalist(UpDataShippingNoteDTO upDataShippingNoteDTO,
											 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

//		QueryWrapper<HyCompanyConfig> queryCompanyWrapper = new QueryWrapper();
//		queryCompanyWrapper.ne("park_report_platform_type",0);
//		if(StringUtils.isNotBlank(upDataShippingNoteDTO.getCompanyId())){
//			queryCompanyWrapper.eq("company_id", upDataShippingNoteDTO.getCompanyId());
//		}
//		queryCompanyWrapper.orderByAsc("park_report_platform_type");
//		List<HyCompanyConfig> companyConfigs = companyConfigService.list(queryCompanyWrapper);
//		if(CollectionUtils.isEmpty(companyConfigs)){
//			return Result.error("园区上报公司配置信息不存在!");
//		}

//		HyCompanyConfig companyConfig = companyConfigs.get(0);
		QueryWrapper<UpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		if (userUtil.isSys()) {
			if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCustomerId())){
				queryWrapper.eq("shipping.customer_id",upDataShippingNoteDTO.getCustomerId());
			}
		} else {
			return Result.error("当前登录角色无淮北上报权限");
		}

		setHuaibeiQueryWrapper(upDataShippingNoteDTO, queryWrapper);

		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<UpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<UpDataShippingNoteDTO> pageList = jgService.getHuaibeiUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}


	@ApiOperation(value="鄂尔多斯数据上报信息-分页列表查询", notes="鄂尔多斯数据上报信息-分页列表查询")
	@GetMapping(value = "/ordosupdatalist")
	public Result<?> queryOrdosUpdatalist(UpDataShippingNoteDTO upDataShippingNoteDTO,
											@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

//		QueryWrapper<HyCompanyConfig> queryCompanyWrapper = new QueryWrapper();
//		queryCompanyWrapper.ne("park_report_platform_type",0);
//		if(StringUtils.isNotBlank(upDataShippingNoteDTO.getCompanyId())){
//			queryCompanyWrapper.eq("company_id", upDataShippingNoteDTO.getCompanyId());
//		}
//		queryCompanyWrapper.orderByAsc("park_report_platform_type");
//		List<HyCompanyConfig> companyConfigs = companyConfigService.list(queryCompanyWrapper);
//		if(CollectionUtils.isEmpty(companyConfigs)){
//			return Result.error("园区上报公司配置信息不存在!");
//		}

		QueryWrapper<UpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		if (userUtil.isSys()) {
			if(StringUtils.isNotEmpty(upDataShippingNoteDTO.getCustomerId())){
				queryWrapper.eq("shipping.customer_id",upDataShippingNoteDTO.getCustomerId());
			}
		} else {
			return Result.error("当前登录角色无鄂尔多斯市监管上报权限");
		}

		setOrdosQueryWrapper(upDataShippingNoteDTO, queryWrapper);

		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<UpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<UpDataShippingNoteDTO> pageList = jgService.getOrdosUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}



	@AutoLog(value = "监管平台-批量上报")
	@ApiOperation(value="监管平台-批量上报", notes="监管平台-批量上报")
	@PutMapping(value = "/uploadBatch")
	public Result<?> uploadBatch(@RequestBody JSONObject jSONObject) {
		String ids = jSONObject.getString("ids");
		List<String> idStrings = Arrays.asList(ids.split(","));
		if (CollectionUtils.isEmpty(idStrings)) {
			return Result.error("推送数据为空!");
		}
		List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIds(idStrings);
		if (CollectionUtils.isEmpty(shippingNotes)) {
			return Result.error("推送数据为空!");
		}

		log.info("监管批量上报运单, {}",
				shippingNotes.stream().map(HyShippingNote::getShippingNoteNumber).collect(Collectors.toList()));

		long payCount = shippingNotes.stream().filter(s -> s.getShippingPayStatus().equals(ShippingPayStatus.支付完成.getValue())).count();
		if (payCount < shippingNotes.size()) {
			return Result.error("上报失败: 当前上报运单中存在未支付的运单");
		}

		int uploadCount = 0;
		for (HyShippingNote hyShippingNote : shippingNotes) {
			try {
				Result rShipping = jgService.uploadShippingNote(hyShippingNote);
				Result rCapital = jgService.uploadCapitalFlow(hyShippingNote);
				log.info("监管批量上报运单, {} , {} {}",
						hyShippingNote.getShippingNoteNumber(), JSONUtil.toJsonStr(rShipping), JSONUtil.toJsonStr(rCapital));
				uploadCount++;
				sleep(500);
			} catch (Exception e) {
				log.error("监管批量上报运单失败{} , {}", hyShippingNote.getShippingNoteNumber(), e.getMessage());
			}
		}

		if (uploadCount < shippingNotes.size()) {
			return Result.error("操作完成：部分运单上报失败，请查看运单上报状态!");
		}

		return Result.OK("批量上报成功!");
	}

	@AutoLog(value = "监管平台数据上报信息")
	@ApiOperation(value="监管平台数据上报信息", notes="监管平台数据上报信息")
	@PutMapping(value = "/allUpload")
	public Result<?> allUpload(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if(hyShippingNote == null){
			return  Result.error("运单不存在!");
		}
		hyShippingNote.setTjUpType(upDataShippingNoteDTO.getTjUpType());
		boolean upst = jgService.uploadByCompany( hyShippingNote);
		return upst ? Result.OK("上报成功!") : Result.error("上报失败!") ;
	}


	@AutoLog(value = "监管平台数据上报运单")
	@ApiOperation(value="监管平台数据上报运单", notes="监管平台数据上报运单")
	@PutMapping(value = "/shippingNoteUpload")
	public Result<?> shippingNoteUpload(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if(hyShippingNote == null){
			return  Result.error("运单不存在!");
		}

		return jgService.uploadShippingNote(hyShippingNote);
	}

	@AutoLog(value = "监管平台数据上报资金流水")
	@ApiOperation(value="监管平台数据上报资金流水", notes="监管平台数据上报资金流水")
	@PutMapping(value = "/capitalFlowUpload")
	public Result<?> capitalFlowUpload(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if(hyShippingNote == null){
			return  Result.error("运单不存在!");
		}

		return jgService.uploadCapitalFlow(hyShippingNote);
	}

	//Added start at 2023-10-23 for 增加园区运单/资金流水上报
	@Autowired
	private IHyShippingParkReportService hyShippingParkReportService;
	@AutoLog(value = "园区数据批量上报运单和资金流水")
	@ApiOperation(value="园区数据批量上报运单和资金流水", notes="园区数据批量上报运单和资金流水")
	@PutMapping(value = "/uploadAllParkShippingNote")
	public Result<?> uploadAllParkShippingNote(@RequestBody JSONObject jSONObject) {

		List<HyShippingNote> hyShippingNoteList = hyShippingNoteService.list(new LambdaQueryWrapper<HyShippingNote>()
				.ne(HyShippingNote::getDelFlag, 1));
		for (HyShippingNote hyShippingNote : hyShippingNoteList) {
			try {
				if (null == this.hyShippingParkReportService.getById(hyShippingNote.getId())) {
					Result rShipping = jgService.uploadParkShippingNote(hyShippingNote);
					sleep(2000);
					Result rCapital = jgService.uploadParkCapitalFlow(hyShippingNote, null);
					sleep(1000);
				} else {
					log.warn("运单{}已上报，忽略此运单", hyShippingNote.getShippingNoteNumber());
				}
			} catch (Exception e) {
				log.error("批量上报运单失败: {} ，----继续上报下一运单", hyShippingNote.getShippingNoteNumber());
			}
		}

		return Result.OK("批量上报成功!");
	}

	@AutoLog(value = "园区数据上报运单")
	@ApiOperation(value="园区数据上报运单", notes="园区数据上报运单")
	@PutMapping(value = "/uploadParkShippingNote")
	public Result<?> uploadParkShippingNote(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return  Result.error("运单不存在!");
		}

		return jgService.uploadParkShippingNote(hyShippingNote);
	}

	@AutoLog(value = "园区数据上报资金流水")
	@ApiOperation(value="园区数据上报资金流水", notes="园区数据上报资金流水")
	@PutMapping(value = "/uploadParkCapitalFlow")
	public Result<?> uploadParkCapitalFlow(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return  Result.error("运单不存在!");
		}

		return jgService.uploadParkCapitalFlow(hyShippingNote, upDataShippingNoteDTO.getBankHdImg());
	}
	//Added end at 2023-10-23 for 增加园区运单/资金流水上报


	@AutoLog(value = "中交产业园区平台运单效验")
	@ApiOperation(value="中交产业园区平台运单效验", notes="中交产业园区平台运单效验")
	@PostMapping(value = "/validateShippingNote")
	public Result<?> validateShippingNote(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		return jgService.checkParkShippingNote(upDataShippingNoteDTO);
		//return upst ? Result.OK("通过!") : Result.error("效验失败!") ;
	}

	@AutoLog(value = "中交产业园区平台运单申诉")
	@ApiOperation(value="中交产业园区平台运单申诉", notes="中交产业园区平台运单申诉")
	@PostMapping(value = "/appealShippingNote")
	public Result<?> appealShippingNote(@RequestBody JSONObject jSONObject) {
		String id = jSONObject.getString("id");
		if (StringUtils.isBlank(id)) {
			return  Result.error("申诉运单ID不能为空!");
		}

		HyShippingNote hyShippingNote = hyShippingNoteService.getById(id);
		if (hyShippingNote == null) {
			return  Result.error("申诉运单不存在!");
		}

		String appealContext = jSONObject.getString("appealContext");
		if (StringUtils.isBlank(appealContext)) {
			return  Result.error("申诉内容不能为空!");
		}

		String appealImg = jSONObject.getString("appealImg");
		if (StringUtils.isBlank(appealImg)) {
			return  Result.error("申诉图片不能为空!");
		}

		return jgService.appealShippingNote(hyShippingNote, appealContext, appealImg);
	}

	@AutoLog(value = "忻州平台运单申诉查询")
	@ApiOperation(value="忻州平台运单申诉查询", notes="忻州平台运单申诉查询")
	@PostMapping(value = "/appealShippingNoteQuery")
	public Result<?> appealShippingNoteQuery(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		return jgService.appealShippingNoteQuery( upDataShippingNoteDTO);
	}

	//endregion

	@AutoLog(value = "淮北监管平台-批量上报")
	@ApiOperation(value = "淮北监管平台-批量上报", notes = "淮北监管平台-批量上报")
	@PutMapping(value = "/uploadBatchHuaibei")
	public Result<?> uploadBatchHuaibei(@RequestBody JSONObject jSONObject) {
		String ids = jSONObject.getString("ids");
		List<String> idStrings = Arrays.asList(ids.split(","));
		if (CollectionUtils.isEmpty(idStrings)) {
			return Result.error("推送数据为空!");
		}
		List<HyShippingNote> shippingNotes = hyShippingNoteService.listByIds(idStrings);
		if (CollectionUtils.isEmpty(shippingNotes)) {
			return Result.error("推送数据为空!");
		}

		log.info("监管批量上报运单, {}",
				shippingNotes.stream().map(HyShippingNote::getShippingNoteNumber).collect(Collectors.toList()));

		long payCount = shippingNotes.stream().filter(s -> s.getShippingPayStatus().equals(ShippingPayStatus.支付完成.getValue())).count();
		if (payCount < shippingNotes.size()) {
			return Result.error("上报失败: 当前上报运单中存在未支付的运单");
		}

		int uploadCount = 0;
		for (HyShippingNote hyShippingNote : shippingNotes) {
			try {
				Result rShipping = jgService.uploadShippingNoteHuaibei(hyShippingNote);
				Result rCapital = jgService.uploadCapitalFlowHuaibei(hyShippingNote);
				log.info("监管批量上报运单, {} , {} {}",
						hyShippingNote.getShippingNoteNumber(), JSONUtil.toJsonStr(rShipping), JSONUtil.toJsonStr(rCapital));
				uploadCount++;
				sleep(500);
			} catch (Exception e) {
				log.error("监管批量上报运单失败{} , {}", hyShippingNote.getShippingNoteNumber(), e.getMessage());
			}
		}

		if (uploadCount < shippingNotes.size()) {
			return Result.error("操作完成：部分运单上报失败，请查看运单上报状态!");
		}

		return Result.OK("批量上报成功!");
	}


	@AutoLog(value = "淮北监管平台数据上报运单")
	@ApiOperation(value = "淮北监管平台数据上报运单", notes = "淮北监管平台数据上报运单")
	@PutMapping(value = "/shippingNoteUploadHuaibei")
	public Result<?> shippingNoteUploadHuaibei(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return Result.error("运单不存在!");
		}

		return jgService.uploadShippingNoteHuaibei(hyShippingNote);
	}

	@AutoLog(value = "淮北监管平台数据上报资金流水")
	@ApiOperation(value = "淮北监管平台数据上报资金流水", notes = "淮北监管平台数据上报资金流水")
	@PutMapping(value = "/capitalFlowUploadHuaibei")
	public Result<?> capitalFlowUploadHuaibei(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return Result.error("运单不存在!");
		}

		return jgService.uploadCapitalFlowHuaibei(hyShippingNote);
	}


	//region 万金油数据上报

	@ApiOperation(value="万金油外调车ETC信息-分页列表查询", notes="万金油外调车ETC信息-分页列表查询")
	@GetMapping(value = "/nucaretclist")
	public Result<?> queryNucarEtcList(HyShippingNoteDTO shippingNoteDTO,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<HyShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		//queryWrapper.eq("hy_shipping_note.order_mode", "1");
		queryWrapper.eq("hy_shipping_note.shipping_status", "5");
		queryWrapper.in("hy_shipping_note.shipping_pay_status", "4", "5", "6");


		if(StringUtils.isNotEmpty(shippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("hy_shipping_note.company_id", shippingNoteDTO.getCompanyId());
		}



		if(StringUtils.isNotEmpty(shippingNoteDTO.getCustomerId())){
			queryWrapper.eq("hy_shipping_note.customer_id",shippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(shippingNoteDTO.getOrderId())){
			queryWrapper.eq("hy_shipping_note.order_id",shippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(shippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("hy_shipping_note.shipping_note_number",shippingNoteDTO.getShippingNoteNumber());
		}
		if(StringUtils.isNotEmpty(shippingNoteDTO.getTransportationDistance())){
			queryWrapper.ge("hy_shipping_note.transportation_distance",Integer.valueOf(shippingNoteDTO.getTransportationDistance()));
		}
		//司机
		if(StringUtils.isNotEmpty(shippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", shippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(shippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", shippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(shippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("v.vehicle_number",shippingNoteDTO.getVehicleNumber());
		}

		//未上传成功
		if(StringUtils.equals("1" ,shippingNoteDTO.getUploadStatusType())){
			queryWrapper.ne("hy_shipping_note.upload_nucar_etc","1");
		}else{
			queryWrapper.eq("hy_shipping_note.upload_nucar_etc","1");
			queryWrapper.eq("hy_shipping_note.etc_type",1);
			//开票成功
//			 queryWrapper.eq("hy_shipping_note.waybill_status","3");
			if(StringUtils.isNotEmpty(shippingNoteDTO.getWaybillStatus())){
				queryWrapper.eq("hy_shipping_note.waybill_status",shippingNoteDTO.getWaybillStatus());
			}
		}
		if(StringUtils.isNotEmpty(shippingNoteDTO.getUploadEtcDate_begin())){
			queryWrapper.ge("hy_shipping_note.upload_etc_date",shippingNoteDTO.getUploadEtcDate_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(shippingNoteDTO.getUploadEtcDate_end() )){
			queryWrapper.le("hy_shipping_note.upload_etc_date",shippingNoteDTO.getUploadEtcDate_end()+" 23:59:59" );
		}

		if(StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("hy_shipping_note.pay_dt",shippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("hy_shipping_note.pay_dt",shippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}

		queryWrapper.orderByDesc("hy_shipping_note.pay_dt");
		Page<HyShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<HyShippingNoteDTO> pageList = shippingNoteEtcService.getPageList(page, queryWrapper);
		for (HyShippingNoteDTO dto : pageList.getRecords()) {
			NucarEtcResponderDTO nucarEtcResponderDTO = nucarfEtcService.findInvoiceByNum(dto.getShippingNoteNumber());
			if (nucarEtcResponderDTO!=null){
				dto.setWaybillStatus(nucarEtcResponderDTO.getWaybillStatus().toString());
			}

		}
		return Result.OK(pageList);
	}



	@AutoLog(value = "万金油外调车ETC开票上报")
	@ApiOperation(value="万金油外调车ETC开票上报", notes="万金油外调车ETC开票上报")
	@PutMapping(value = "/uploadNucarEtc")
	public Result<?> uploadNucarEtc(@RequestBody JSONObject jo) throws Exception {
		HyConfig systemConfig = HyConfigUtils.getConfig();
		if(systemConfig == null || !systemConfig.getEtcwdIzUse()){
			throw new JeecgBootException("万金油外调车ETC服务暂未开启,请联系管理。");
		}
		String ids = jo.getString("ids");
		List<HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIds(Arrays.asList(ids.split(",")));
		for (HyShippingNote hyShippingNote : hyShippingNotes) {

			if (StrUtil.isEmpty(hyShippingNote.getShippingContract())) {
				return Result.error("上报失败: " + hyShippingNote.getShippingNoteNumber() + ",运单未找到运输合同!");
			}

			if(!"1".equals(hyShippingNote.getUploadNucarEtc())){
				Boolean reportResult = shippingNoteEtcService.uploadNucarEtc(hyShippingNote);
				hyShippingNote.setUploadNucarEtc(reportResult ? "1" : "2");
				hyShippingNote.setUploadEtcDate(new Date());
				hyShippingNote.setEtcType(1);
			}
		}
		return hyShippingNoteService.updateBatchById(hyShippingNotes) ? Result.OK("上报成功!") : Result.error("上报失败!");
	}

//	@AutoLog(value = "万金油外调车ETC开票查询")
	@ApiOperation(value="万金油外调车ETC开票查询", notes="万金油外调车ETC开票查询")
	@GetMapping(value = "/queryEtcInfo")
	public Result<?> queryEtcInfo(@RequestParam(name="id",required=true) String id) {
		HyShippingNote shippingNote = hyShippingNoteService.getById(id);
		if(shippingNote== null){
			throw new JeecgBootException("未找到对应数据");
		}
		NucarEtcResponderDTO reportResult = shippingNoteEtcService.queryEtcInfo(shippingNote);
		if(reportResult != null && reportResult.getWaybillStatus() != null ){
			return Result.OK(reportResult);
		}
		return Result.error("该运单开票中!");
	}

	@ApiOperation(value="路耕ETC信息-分页列表查询", notes="路耕ETC信息-分页列表查询")
	@GetMapping(value = "/feecloudsEtclist")
	public Result<?> queryFeecloudsEtcList(EtcFeecloudsShippingNoteDTO etcFeecloudsShippingNoteDTO,
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<EtcFeecloudsShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		//queryWrapper.eq("hy_shipping_note.order_mode", "1");
		queryWrapper.eq("hy_shipping_note.shipping_status", "5");
		queryWrapper.eq("hy_shipping_note.shipping_pay_status", "6");

		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("hy_shipping_note.customer_id",etcFeecloudsShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getOrderId())){
			queryWrapper.eq("hy_shipping_note.order_id",etcFeecloudsShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("hy_shipping_note.shipping_note_number",etcFeecloudsShippingNoteDTO.getShippingNoteNumber());
		}
		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getTransportationDistance())){
			queryWrapper.ge("hy_shipping_note.transportation_distance",Integer.valueOf(etcFeecloudsShippingNoteDTO.getTransportationDistance()));
		}
		//司机
		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("d.driver_name", etcFeecloudsShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("d.telephone", etcFeecloudsShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("v.vehicle_number",etcFeecloudsShippingNoteDTO.getVehicleNumber());
		}


		if(etcFeecloudsShippingNoteDTO.getOwnCar() != null){
			queryWrapper.eq("etc_order.own_car",etcFeecloudsShippingNoteDTO.getOwnCar());
		}

		if(etcFeecloudsShippingNoteDTO.getOrderState() != null){
			queryWrapper.eq("etc_order.state",etcFeecloudsShippingNoteDTO.getOrderState());
		}

		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getTradeId())){
			queryWrapper.like("etc_trade.trade_id",etcFeecloudsShippingNoteDTO.getTradeId());
		}

		if(etcFeecloudsShippingNoteDTO.getTradeStatus() != null){
			queryWrapper.eq("etc_trade.state",etcFeecloudsShippingNoteDTO.getTradeStatus());
		}

		if(StringUtils.isNotBlank(etcFeecloudsShippingNoteDTO.getTradeFlowNo())){
			queryWrapper.like("etc_trade.trade_flow_no",etcFeecloudsShippingNoteDTO.getTradeFlowNo());
		}

		//未上传成功
		if(StringUtils.equals("1" ,etcFeecloudsShippingNoteDTO.getUploadStatusType())){
			queryWrapper.ne("hy_shipping_note.upload_nucar_etc","1");
		}else{
			queryWrapper.eq("hy_shipping_note.upload_nucar_etc","1");
			queryWrapper.eq("hy_shipping_note.etc_type",2);
		}

		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("hy_shipping_note.pay_dt",etcFeecloudsShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(etcFeecloudsShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("hy_shipping_note.pay_dt",etcFeecloudsShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}

		queryWrapper.orderByDesc("hy_shipping_note.pay_dt");
		Page<EtcFeecloudsShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<EtcFeecloudsShippingNoteDTO> pageList = shippingNoteEtcService.getEtcFeecloudsList(page, queryWrapper);
		return Result.OK(pageList);
	}



	@AutoLog(value = "路耕外调车ETC开票上报")
	@ApiOperation(value="路耕外调车ETC开票上报", notes="路耕外调车ETC开票上报")
	@PutMapping(value = "/uploadFeecloudsEtc")
	public Result<?> uploadFeecloudsEtc(@RequestBody JSONObject jo) {
		HyConfig systemConfig = HyConfigUtils.getConfig();
		if (systemConfig == null || !systemConfig.getEtcFeecloudsIzUse()) {
			return Result.error("路耕ETC上报服务暂未开启,请联系管理员。");
		}
		String ids = jo.getString("ids");
		List<HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIds(Arrays.asList(ids.split(",")));
		for (HyShippingNote hyShippingNote : hyShippingNotes) {
			if(!"1".equals(hyShippingNote.getUploadNucarEtc())){
				Boolean reportResult = shippingNoteEtcService.uploadFeecloudsEtc(hyShippingNote);
				hyShippingNote.setUploadNucarEtc(reportResult ? "1" : "2");
				hyShippingNote.setUploadEtcDate(new Date());
				hyShippingNote.setEtcType(2);
			}
		}
		return hyShippingNoteService.updateBatchById(hyShippingNotes) ? Result.OK("上报成功!") : Result.error("上报失败!");
	}

	//endregion

	//region 道达上报

	@ApiOperation(value="道达上报状态信息-分页列表查询", notes="道达上报状态信息-分页列表查询")
	@GetMapping(value = "/daodaupdatalist")
	public Result<?> queryDaodaUpdataPageList(DaodaUpDataShippingNoteDTO daodaUpDataShippingNoteDTO,
											  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<DaodaUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");
		//queryWrapper.eq("shipping.order_mode", "1");

		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", daodaUpDataShippingNoteDTO.getCompanyId());
		}else{
			//申报公司：河北一六八网络科技有限公司 宏瑞
			HyCompanyDTO hyCompany   = hyCompanyService.getCompanyAndConfig(
					new HyCompanyDTO()
							.setCompanyId(daodaUpDataShippingNoteDTO.getCompanyId())
							.setReportPlatformType(ReportPlatformEnum.REPORT_PLATFORM_HB.getCode())
			);
			if(hyCompany != null){
				queryWrapper.eq("shipping.company_id", hyCompany.getId());
			}
		}

		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",daodaUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",daodaUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",daodaUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", daodaUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", daodaUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", daodaUpDataShippingNoteDTO.getVehicleNumber());
		}
		//运单轨迹上报状态
		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getOrderLineUploadStatus())){
			queryWrapper.eq("daodaorder.upload_status",daodaUpDataShippingNoteDTO.getOrderLineUploadStatus());
		}
		queryWrapper.eq("bill.bill_type","1");

		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getUploadStatusType())){
			queryWrapper.nested(
					qw-> qw.eq("driver.daoda_upload_status","1")
							.eq("vehicle.daoda_upload_status","1")
							.eq("shipping.daoda_upload_status","1")
							.eq("daodaorder.upload_status","1")
			);

		}else{
			queryWrapper.nested(
					qw-> qw.ne("driver.daoda_upload_status","1")
							.or().ne("vehicle.daoda_upload_status","1")
							.or().ne("shipping.daoda_upload_status","1")
							.or().ne("daodaorder.upload_status","1")
			);
		}


		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",daodaUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(daodaUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",daodaUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<DaodaUpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<DaodaUpDataShippingNoteDTO> pageList = shippingNoteDaodaService.getDaodaUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}


	@AutoLog(value = "道达上报")
	@ApiOperation(value="道达上报", notes="道达上报")
	@PutMapping(value = "/daodaUpload")
	public Result<?> daodaUpload(@RequestBody DaodaUpDataShippingNoteDTO hyShippingNote) {

		Boolean resl = true;
		if(!StringUtils.equals("1" ,hyShippingNote.getDriverDaodaUploadStatus())){
			resl = hyDriverService.uploadDaoda(new HyDriver().setId(hyShippingNote.getDriverId()));
		}

		if(!StringUtils.equals("1" ,hyShippingNote.getVehicleDaodaUploadStatus())) {
			resl = hyVehicleService.uploadDaoda(new HyVehicle()
					.setId(hyShippingNote.getVehicleId())
					.setDriverId(hyShippingNote.getDriverId())
			);
		}
		if(!StringUtils.equals("1" ,hyShippingNote.getDaodaUploadStatus())) {
			resl  = shippingNoteDaodaService.uploadDaoda(hyShippingNote);
		}
		if(!StringUtils.equals("1" ,hyShippingNote.getOrderLineUploadStatus())) {
			resl  = shippingNoteDaodaService.uploadDaodaOrderLine(hyShippingNote);
		}
		return resl ? Result.OK("上报成功!") : Result.error("上报失败!");
	}

	//endregion

	//region 通达上报

	@ApiOperation(value="通达上报状态信息-分页列表查询", notes="通达上报状态信息-分页列表查询")
	@GetMapping(value = "/tongdaupdatalist")
	public Result<?> queryTongdaUpdataPageList(TongdaUpDataShippingNoteDTO tongdaUpDataShippingNoteDTO,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		HyConfig systemConfig = HyConfigUtils.getConfig();
		if(systemConfig == null || !systemConfig.getTongdaIzUse()){
			throw new JeecgBootException("通达服务暂未开启,请联系管理。");
		}

		QueryWrapper<TongdaUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");
		//queryWrapper.eq("shipping.order_mode", "1");

		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", tongdaUpDataShippingNoteDTO.getCompanyId());
		}

		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",tongdaUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",tongdaUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",tongdaUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", tongdaUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", tongdaUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", tongdaUpDataShippingNoteDTO.getVehicleNumber());
		}

		queryWrapper.eq("bill.bill_type","1");

		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getUploadStatusType())){
			queryWrapper.eq("shipping.tongda_upload_status","1");
		}else{
			queryWrapper.ne("shipping.tongda_upload_status","1");
		}


		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",tongdaUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(tongdaUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",tongdaUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<TongdaUpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<TongdaUpDataShippingNoteDTO> pageList = shippingNoteTongdaService.getTongdaUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}


	@AutoLog(value = "通达上报")
	@ApiOperation(value="通达上报", notes="通达上报")
	@PutMapping(value = "/tongdaUpload")
	public Result<?> tongdaUpload(@RequestBody TongdaUpDataShippingNoteDTO hyShippingNote) {
		HyConfig systemConfig = HyConfigUtils.getConfig();
		if(systemConfig == null || !systemConfig.getTongdaIzUse()){
			throw new JeecgBootException("通达服务暂未开启,请联系管理。");
		}
		return shippingNoteTongdaService.uploadTongda(hyShippingNote) ? Result.OK("上报成功!") : Result.error("上报失败!");
	}

	@AutoLog(value = "通达-批量上报")
	@ApiOperation(value="通达-批量上报", notes="通达-批量上报")
	@PutMapping(value = "/tongdaUloadBatch")
	public Result<?> tongdaUloadBatch(@RequestBody JSONObject jSONObject) {
		String ids = jSONObject.getString("ids");
		String domainBank = jSONObject.getString("domainBank");
		String domain = jSONObject.getString("domain");

		List <String> idStrings =  Arrays.asList(ids.split(","));
		if(CollectionUtils.isEmpty(idStrings)){
			return Result.error("推送数据为空!");
		}
		QueryWrapper<TongdaUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();
		queryWrapper.in("shipping.id",idStrings);
		queryWrapper.eq("bill.bill_type","1");
		Page<TongdaUpDataShippingNoteDTO> page = new Page<>(1, 200);
		IPage<TongdaUpDataShippingNoteDTO> pageList = shippingNoteTongdaService.getTongdaUpDataPageList(page, queryWrapper);
		List<TongdaUpDataShippingNoteDTO> upDataShippingNoteDTOS = pageList.getRecords().stream().collect(toList());
		if(CollectionUtils.isEmpty(upDataShippingNoteDTOS)){
			return Result.error("推送数据为空!");
		}

		int i = 0;
		for (TongdaUpDataShippingNoteDTO  tongdaUpDataShippingNoteDTO: upDataShippingNoteDTOS){
			tongdaUpDataShippingNoteDTO.setDomain(domain);
			tongdaUpDataShippingNoteDTO.setDomainBank(domainBank);
			boolean res = shippingNoteTongdaService.uploadTongda(tongdaUpDataShippingNoteDTO);
			if(res){
				i++;
			}
		}
		return i == upDataShippingNoteDTOS.size() ?  Result.OK("上报成功!") : Result.error("上报失败!") ;
	}

	@AutoLog(value = "通达上报-运单补充轨迹信息")
	@ApiOperation(value="通达上报-运单补充轨迹信息", notes="通达上报-运单补充轨迹信息")
	@PostMapping(value = "/tongdaWaybillSupplement")
	public Result<?> tongdaWaybillSupplement(@RequestBody JSONObject jSONObject) {


		String waybillNo = jSONObject.getString("shippingNoteNumber");
		String fileUrl = jSONObject.getString("fileUrl");
		if(StringUtils.isBlank(fileUrl)){
			return  Result.error("轨迹文件不能为空");
		}
		QueryWrapper<HyShippingNote> queryWrapper = new QueryWrapper();
		queryWrapper.eq("shipping_note_number",waybillNo);
		HyShippingNote hyShippingNote = hyShippingNoteService.getOne(queryWrapper);
		if(hyShippingNote == null ){
			return  Result.error("运单信息不存在");
		}
		hyShippingNote.setTrajectoryFile(fileUrl);

		return hyShippingNoteService.updateById(hyShippingNote) ? Result.OK("补充成功!") : Result.error("补充失败");
	}

	//endregion


	//region 马帮上报

	@ApiOperation(value="马帮上报状态信息-分页列表查询", notes="马帮上报状态信息-分页列表查询")
	@GetMapping(value = "/mabangupdatalist")
	public Result<?> queryMaBangUpdataPageList(MaBangUpDataShippingNoteDTO maBangUpDataShippingNoteDTO,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		if(!HyCfgUtil.isUsed("mabang_daikai")){
			throw new JeecgBootException("马帮服务暂未开启,请联系管理。");
		}

		QueryWrapper<MaBangUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");
		//queryWrapper.eq("shipping.order_mode", "1");

		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", maBangUpDataShippingNoteDTO.getCompanyId());
		}

		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",maBangUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",maBangUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",maBangUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", maBangUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", maBangUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", maBangUpDataShippingNoteDTO.getVehicleNumber());
		}

		queryWrapper.eq("bill.bill_type","1");

		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getUploadStatusType())){
			queryWrapper.eq("mbs.upload_status","1");
		}else{

			queryWrapper.nested(
					qw-> qw.ne("mbs.upload_status","1")
							.or().isNull("mbs.upload_status")
			);

		}


		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",maBangUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(maBangUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",maBangUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<MaBangUpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<MaBangUpDataShippingNoteDTO> pageList = mbShippingService.getMaBangUpDataPageList(page, queryWrapper);

		//如果开启系统脱敏
		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (MaBangUpDataShippingNoteDTO dto : pageList.getRecords()) {
				dto.setBillNum(DataMaskingUtil.bankNumberMasking(dto.getBillNum()));
				dto.setTelephone(DataMaskingUtil.phoneMasking(dto.getTelephone()));
			}
		}

		return Result.OK(pageList);
	}



	@AutoLog(value = "马帮上报")
	@ApiOperation(value="马帮上报", notes="马帮上报")
	@PutMapping(value = "/mabangUpload")
	public Result<?> mabangUpload(@RequestBody MaBangUpDataShippingNoteDTO hyShippingNote) {
		if(!HyCfgUtil.isUsed("mabang_daikai")){
			throw new JeecgBootException("马帮服务暂未开启,请联系管理。");
		}
		return mbShippingService.uploadMabang(hyShippingNote) ? Result.OK("上报成功!") : Result.error("上报失败!");
	}

	@AutoLog(value = "马帮-批量上报")
	@ApiOperation(value="马帮-批量上报", notes="马帮-批量上报")
	@PutMapping(value = "/mabangUloadBatch")
	public Result<?> mabangUloadBatch(@RequestBody JSONObject jSONObject) {
		String ids = jSONObject.getString("ids");
		List <String> idStrings =  Arrays.asList(ids.split(","));
		if(CollectionUtils.isEmpty(idStrings)){
			return Result.error("推送数据为空!");
		}
		QueryWrapper<MaBangUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();
		queryWrapper.in("shipping.id",idStrings);
		queryWrapper.eq("bill.bill_type","1");
		Page<MaBangUpDataShippingNoteDTO> page = new Page<>(1, 200);
		IPage<MaBangUpDataShippingNoteDTO> pageList =  mbShippingService.getMaBangUpDataPageList(page, queryWrapper);
		List<MaBangUpDataShippingNoteDTO> upDataShippingNoteDTOS = pageList.getRecords().stream().collect(toList());
		if(CollectionUtils.isEmpty(upDataShippingNoteDTOS)){
			return Result.error("推送数据为空!");
		}

		int i = 0;
		for (MaBangUpDataShippingNoteDTO  maBangUpDataShippingNoteDTO: upDataShippingNoteDTOS){
			boolean res = mbShippingService.uploadMabang(maBangUpDataShippingNoteDTO);
			if(res){
				i++;
			}
		}
		return i == upDataShippingNoteDTOS.size() ?  Result.OK("上报成功!") : Result.error("上报失败!") ;
	}


	//endregion

	//region 梵运上报


	@ApiOperation(value="梵运上报状态信息-分页列表查询", notes="梵运上报状态信息-分页列表查询")
	@GetMapping(value = "/fyupdatalist")
	public Result<?> queryFanyunUpdataPageList(FyUpDataShippingNoteDTO fyUpDataShippingNoteDTO,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		if(!HyCfgUtil.isUsed("fanyun_daikai")){
			throw new JeecgBootException("梵运服务暂未开启,请联系管理。");
		}

		QueryWrapper<FyUpDataShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");
		//queryWrapper.eq("shipping.order_mode", "1");

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", fyUpDataShippingNoteDTO.getCompanyId());
		}

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",fyUpDataShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",fyUpDataShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",fyUpDataShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", fyUpDataShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", fyUpDataShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", fyUpDataShippingNoteDTO.getVehicleNumber());
		}

		queryWrapper.eq("bill.bill_type","1");

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getUploadStatusType())){
			if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getIzConfirm())){
				queryWrapper.eq("fys.iz_confirm",fyUpDataShippingNoteDTO.getIzConfirm());
			}
			queryWrapper.eq("fys.upload_status","1");
		}else{
			queryWrapper.nested(
					qw-> qw.ne("fys.upload_status","1")
							.or().isNull("fys.upload_status")
			);
		}

		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",fyUpDataShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(fyUpDataShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",fyUpDataShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}


		queryWrapper.orderByDesc("shipping.pay_dt");
		Page<FyUpDataShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<FyUpDataShippingNoteDTO> pageList = fyShippingService.getFanyunUpDataPageList(page, queryWrapper);
		return Result.OK(pageList);
	}



	@AutoLog(value = "梵运上报")
	@ApiOperation(value="梵运上报", notes="梵运上报")
	@PutMapping(value = "/fanyunUpload")
	public Result<?> fanyunUpload(@RequestBody FyUpDataShippingNoteDTO hyShippingNote) {
		if(!HyCfgUtil.isUsed("fanyun_daikai")){
			throw new JeecgBootException("梵运服务暂未开启,请联系管理。");
		}

		return fyShippingService.uploadFanyun(hyShippingNote);
	}

	@AutoLog(value = "梵运上报-确认运单")
	@ApiOperation(value="梵运上报-确认运单", notes="梵运上报-确认运单")
	@PutMapping(value = "/confirmShipping")
	public Result<?> confirmShipping(@RequestBody JSONObject jo) {
		if(!HyCfgUtil.isUsed("fanyun_daikai")){
			throw new JeecgBootException("梵运服务暂未开启,请联系管理。");
		}
		String ids = jo.getString("ids");
		if(StringUtils.isBlank(ids)){
			return  Result.error("确认信息不能为空!");
		}
		List<HyFyShipping> hyFyShippings = fyShippingService.listByIds(Arrays.asList(ids.split(",")));
		if(hyFyShippings.size() < 1){
			return  Result.error("确认信息不能为空!");
		}

		List<String> shippingNoteNumbers = new ArrayList<>();
		for (HyFyShipping hyFyShipping : hyFyShippings) {
			if(!"1".equals(hyFyShipping.getIzConfirm())){
				shippingNoteNumbers.add(hyFyShipping.getFanyunWaybillNumber());
			}
		}
		boolean res = fyShippingService.signOff0rder(shippingNoteNumbers) ;
		if(res){
			for (HyFyShipping hyFyShipping : hyFyShippings) {
				hyFyShipping.setIzConfirm("1");
			}
			fyShippingService.updateBatchById(hyFyShippings);
		}

		return res ? Result.OK("上报成功!") : Result.error("上报失败!");
	}


	//endregion

	//region 陆交开放平台上报
	/**
	 * 辽宁税务上报，运单列表获取
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="辽宁税务数据上报状态信息-分页列表查询", notes="辽宁税务数据上报状态信息-分页列表查询")
	@GetMapping(value = "/lntaxupdatalist")
	public Result<?> queryLnTaxUpdataPageList(LnTaxShippingNoteDTO shippingNoteDTO,
											  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<LnTaxShippingNoteDTO> queryWrapper = new QueryWrapper();
		//辽税标识,1代表属于辽税运单
		queryWrapper.eq("hy_shipping_note.alct_identification", "1");

		if (userUtil.isSys()) {
			if (StringUtils.isNotEmpty(shippingNoteDTO.getCustomerId())) {
				queryWrapper.eq("hy_shipping_note.customer_id", shippingNoteDTO.getCustomerId());
			}
		} else {
			return Result.error("只有管理员才能查询");
		}

//		if (StringUtils.isNotEmpty(shippingNoteDTO.getCompanyId())) {
//			queryWrapper.eq("hy_shipping_note.company_id", shippingNoteDTO.getCompanyId());
//		} else {
//			queryWrapper.eq("hy_shipping_note.company_id", shippingNoteDTO.getCompanyId());
//		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getOrderId())) {
			queryWrapper.eq("hy_shipping_note.order_id",shippingNoteDTO.getOrderId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getShippingNoteNumber())) {
			queryWrapper.like("hy_shipping_note.shipping_note_number",shippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if (StringUtils.isNotEmpty(shippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("d.driver_name", shippingNoteDTO.getDriverName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("d.telephone", shippingNoteDTO.getTelephone());
		}

		//车辆
		if (StringUtils.isNotEmpty(shippingNoteDTO.getVehicleNumber())) {
			queryWrapper.like("v.vehicle_number", shippingNoteDTO.getVehicleNumber());
		}

		//运单列表中不展示智运宝的运单
		queryWrapper.isNull("hy_shipping_note.vehicle_device");
		queryWrapper.eq("hy_shipping_note.del_flag", "0");

		//Added at 2023-08-21 for 货源号搜索,货源号搜索时的前提是没有货源id搜索条件
//		if (StringUtils.isEmpty(shippingNoteDTO.getOrderId()) && StringUtils.isNotEmpty(shippingNoteDTO.getOrderNum())) {
//			List<HyOrder> hyOrderList = this.hyOrderService.list(new LambdaQueryWrapper<HyOrder>().like(HyOrder::getOrderNum, shippingNoteDTO.getOrderNum()));
//			if (CollUtil.isNotEmpty(hyOrderList)) {
//				List<String> ids = hyOrderList.stream().map(HyOrder::getId).collect(Collectors.toList());
//				queryWrapper.in("hy_shipping_note.order_id", ids);
//			} else {
//				return Result.OK();
//			}
//		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getKeyword())) {
			queryWrapper.and(
					qw -> qw.like("d.driver_name", shippingNoteDTO.getKeyword())
							.or()
							.like("v.vehicle_number", shippingNoteDTO.getKeyword()));
		}

		String byDesc = shippingNoteDTO.getByDesc();
		if (StringUtils.isNotEmpty(byDesc)) {
			String[] bys = byDesc.split(",");
			for (String by : bys) {
				queryWrapper.orderByDesc("hy_shipping_note." + by);
			}

		} else {
			queryWrapper.orderByDesc("hy_shipping_note.create_time");
		}
		queryWrapper.groupBy("hy_shipping_note.id");

		Page<LnTaxShippingNoteDTO> page = new Page<LnTaxShippingNoteDTO>(pageNo, pageSize);
		IPage<LnTaxShippingNoteDTO> pageList = hyShippingNoteService.getLnTaxPageList(page, queryWrapper);

		return Result.OK(pageList);
	}

	@AutoLog(value = "陆交开放平台-创建运单")
	@ApiOperation(value="陆交开放平台-创建运单", notes="陆交开放平台-创建运单")
	@PutMapping(value = "/createShippingNote")
	@Transactional
	public Result<?> createShippingNote(@RequestBody HyShippingNote hyShippingNote) {
		HyShippingNote hyShippingNote1 = this.hyShippingNoteService.getByShippingNoteNumer(hyShippingNote.getShippingNoteNumber());
		if (null == hyShippingNote1) {
			return Result.error("运单不存在");
		}
		return this.shippingNoteApiAlct56Service.createShippingNote(hyShippingNote1);
	}

	@AutoLog(value = "陆交开放平台-补录运单")
	@ApiOperation(value="陆交开放平台-补录运单", notes="陆交开放平台-补录运单")
	@PutMapping(value = "/uploadHistoryShippingNote")
	@Transactional
	public Result<?> uploadHistoryShippingNote(@RequestBody HyShippingNote hyShippingNote) {
		HyShippingNote hyShippingNoteDB = this.hyShippingNoteService.getByShippingNoteNumer(hyShippingNote.getShippingNoteNumber());
		if (null == hyShippingNoteDB) {
			return Result.error("运单不存在");
		}
		return this.shippingNoteApiAlct56Service.uploadHistoryShippingNote(hyShippingNoteDB);
	}

	@AutoLog(value = "陆交开放平台-保存上报税务状态")
	@ApiOperation(value="陆交开放平台-保存上报税务状态", notes="陆交开放平台-保存上报税务状态")
	@PostMapping(value = "/saveAlctShippingNoteStatus")
	public Result<?> saveAlctShippingNoteStatus(@RequestBody HyShippingNote hyShippingNote) {
		return this.shippingNoteApiAlct56Service.saveAlctShippingNoteStatus(hyShippingNote);
	}


	@AutoLog(value = "陆交开放平台-司机获取运单状态")
	@ApiOperation(value="陆交开放平台-司机获取运单状态", notes="陆交开放平台-获取运单状态")
	@GetMapping(value = "/getAlctShippingNoteByDriver")
	public Result<?> getAlctShippingNoteByDriver(HyShippingNote hyShippingNote) {
		return this.shippingNoteApiAlct56Service.getAlctShippingNoteByDriver(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-货主获取运单状态")
	@ApiOperation(value="陆交开放平台-货主获取运单状态", notes="陆交开放平台-获取运单状态")
	@GetMapping(value = "/getAlctShippingNoteByCustomer")
	public Result<?> getAlctShippingNoteByCustomer(HyShippingNote hyShippingNote) {
		return this.shippingNoteApiAlct56Service.getAlctShippingNoteByCustomer(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-银行回单上传")
	@ApiOperation(value="陆交开放平台-银行回单上传", notes="陆交开放平台-银行回单上传")
	@PutMapping(value = "/uploadBankReceipt")
	public Result<?> uploadBankReceipt(@RequestBody LnTaxShippingNoteDTO hyShippingNote) {
		return this.shippingNoteApiAlct56Service.uploadBankReceipt(hyShippingNote);
	}


	@AutoLog(value = "陆交开放平台-取消运单")
	@ApiOperation(value="陆交开放平台-取消运单", notes="陆交开放平台-取消运单")
	@PutMapping(value = "/cancelShippingNote")
	@Transactional
	public Result<?> cancelShippingNote(@RequestBody HyShippingNote hyShippingNote) {
		//hyShippingNote.setShippingStatus(ShippingStatus.已作废.getValue());
		//hyShippingNoteService.updateById(hyShippingNote);
		shippingNoteApiAlct56Service.cancelShippingNote(hyShippingNote);
		return Result.OK("取消运单成功!");
	}

	@AutoLog(value = "陆交开放平台-运单更新运费上传合同")
	@ApiOperation(value="陆交开放平台-运单更新运费上传合同", notes="陆交开放平台-运单更新运费上传合同")
	@PutMapping(value = "/updatePrice")
	public Result<?> updatePrice(@RequestBody HyShippingNote hyShippingNote) {
		return shippingNoteApiAlct56Service.updatePriceAndContract(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-同步轨迹数据")
	@ApiOperation(value="陆交开放平台-同步轨迹数据", notes="陆交开放平台-同步轨迹数据")
	@PutMapping(value = "/synTraces")
	public Result<?> synTraces(@RequestBody HyShippingNote hyShippingNote) {
		return shippingNoteApiAlct56Service.syncTraces(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-补传装货照片")
	@ApiOperation(value="陆交开放平台-补传装货照片", notes="陆交开放平台-补传装货照片")
	@PostMapping(value = "/uploadPickupImage")
	public Result<?> uploadPickupImage(@RequestBody HyShippingNote hyShippingNote) {
		return shippingNoteApiAlct56Service.uploadShippingPickupImage(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-补传卸装照片")
	@ApiOperation(value="陆交开放平台-补传卸装照片", notes="陆交开放平台-补传卸装照片")
	@PostMapping(value = "/uploadUnloadImage")
	public Result<?> uploadUnloadImage(@RequestBody HyShippingNote hyShippingNote) {
		return shippingNoteApiAlct56Service.uploadShippingUnloadImage(hyShippingNote);
	}

	@AutoLog(value = "陆交开放平台-补传回单照片")
	@ApiOperation(value="陆交开放平台-补传卸装照片", notes="陆交开放平台-补传卸装照片")
	@PostMapping(value = "/uploadPodImage")
	public Result<?> uploadPodImage(@RequestBody HyShippingNote hyShippingNote) {
		return shippingNoteApiAlct56Service.uploadShippingPodImage(hyShippingNote);
	}


	//endregion

	//region 快货运平台上报
	@AutoLog(value = "快货运开放平台-同步运输合同")
	@ApiOperation(value="快货运开放平台-同步运输合同", notes="快货运开放平台-同步运输合同")
	@PutMapping(value = "/waybillContract")
	public Result<?> waybillContract(@RequestBody HyShippingNote hyShippingNote) {
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillContract(hyShippingNote);
		return  tosApiBaseResDTO.isSuccess()  ? Result.OK("同步成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}

	@AutoLog(value = "快货运开放平台-同步运单轨迹")
	@ApiOperation(value="快货运开放平台-同步运单轨迹", notes="快货运开放平台-同步运单轨迹")
	@PutMapping(value = "/waybillGeo")
	public Result<?> waybillGeo(@RequestBody HyShippingNote hyShippingNote) {
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillGeo(hyShippingNote);
		return  tosApiBaseResDTO.isSuccess()  ? Result.OK("同步成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}
	@AutoLog(value = "快货运开放平台-已支付运单更新")
	@ApiOperation(value="快货运开放平台-已支付运单更新", notes="快货运开放平台-已支付运单更新")
	@PutMapping(value = "/waybillCreate")
	public Result<?> waybillCreate(@RequestBody HyShippingNote hyShippingNote) {
		TosApiBaseResDTO tosApiBaseResDTO =  shippingNoteTosApiService.waybillCreate(hyShippingNote);
		return tosApiBaseResDTO.isSuccess() ? Result.OK("更新运单成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}

	@AutoLog(value = "快货运开放平台-已支付运单同步资金流水")
	@ApiOperation(value="快货运开放平台-已支付运单同步资金流水", notes="快货运开放平台-已支付运单同步资金流水")
	@PutMapping(value = "/waybillPayment")
	public Result<?> waybillPayment(@RequestBody HyShippingNote hyShippingNote) {
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillPayment(hyShippingNote);
		return tosApiBaseResDTO.isSuccess() ? Result.OK("同步成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}

	@AutoLog(value = "快货运开放平台-运单补充信息")
	@ApiOperation(value="快货运开放平台-运单补充信息", notes="快货运开放平台-运单补充信息")
	@PostMapping(value = "/waybillSupplement")
	public Result<?> waybillSupplement(@RequestBody JSONObject jSONObject) {
		String waybillNo = jSONObject.getString("shippingNoteNumber");
		String remark = jSONObject.getString("remark");
		String fileUrl = jSONObject.getString("fileUrl");
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillSupplement(waybillNo,remark,fileUrl);
		return tosApiBaseResDTO.isSuccess() ? Result.OK("补充成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}

	@AutoLog(value = "快货运开放平台-运单确认")
	@ApiOperation(value="快货运开放平台-运单确认", notes="快货运开放平台-运单确认")
	@PostMapping(value = "/waybillConfirm")
	public Result<?> waybillConfirm(@RequestParam(name="ids",required=true) String ids) {
		List<String> waybillNoList= Arrays.asList(ids.split(","));
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillConfirm(waybillNoList);
		return tosApiBaseResDTO.isSuccess() ? Result.OK("确认成功!") : Result.error(tosApiBaseResDTO.getBiz_message());
	}

	@AutoLog(value = "快货运开放平台-同步托运人合同")
	@ApiOperation(value="快货运开放平台-同步托运人合同", notes="快货运开放平台-同步托运人合同")
	@PutMapping(value = "/waybillCustomerContract")
	public Result<?> waybillCustomerContract(@RequestBody HyShippingNote hyShippingNote) {
		TosApiBaseResDTO tosApiBaseResDTO = shippingNoteTosApiService.waybillCustomerContract(hyShippingNote);
		return  tosApiBaseResDTO.isSuccess()  ? Result.OK("同步成功!") : Result.error("同步失败!");
	}

	//endregion


	@AutoLog(value = "鄂尔多斯-同步轨迹数据")
	@ApiOperation(value="鄂尔多斯-同步轨迹数据", notes="鄂尔多斯-同步轨迹数据")
	@PutMapping(value = "/ordosSynTraces")
	public Result<?> ordosSynTraces(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {

		HyShippingNote hyShippingNote = this.hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (null == hyShippingNote) {
			return Result.error("运单不存在!");
		}

		return this.ordosReportService.reportShippingNote(hyShippingNote, "10", null, null);
	}


	@AutoLog(value = "鄂尔多斯-数据上报运单")
	@ApiOperation(value = "鄂尔多斯-数据上报运单", notes = "鄂尔多斯-数据上报运单")
	@PutMapping(value = "/shippingNoteUploadOrdos")
	public Result<?> shippingNoteUploadOrdos(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return Result.error("运单不存在!");
		}

		return this.ordosReportService.reportShippingNote(hyShippingNote, "20", null, null);
	}

	@AutoLog(value = "鄂尔多斯-数据上报资金流水")
	@ApiOperation(value = "鄂尔多斯-数据上报资金流水", notes = "鄂尔多斯-数据上报资金流水")
	@PutMapping(value = "/capitalFlowUploadOrdos")
	public Result<?> capitalFlowUploadOrdos(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return Result.error("运单不存在!");
		}

		return this.ordosReportService.reportShippingNote(hyShippingNote, "30", null, null);
	}

	@AutoLog(value = "鄂尔多斯-上报开票信息")
	@ApiOperation(value = "鄂尔多斯-上报开票信息", notes = "鄂尔多斯-上报开票信息")
	@PutMapping(value = "/invoiceUploadOrdos")
	public Result<?> invoiceUploadOrdos(@RequestBody UpDataShippingNoteDTO upDataShippingNoteDTO) {
		HyShippingNote hyShippingNote = hyShippingNoteService.getById(upDataShippingNoteDTO.getId());
		if (hyShippingNote == null) {
			return Result.error("运单不存在!");
		}

		return this.ordosReportService.reportShippingNote(hyShippingNote, "40", null, null);
	}

//	@AutoLog(value = "鄂尔多斯装货上报")
//	@ApiOperation(value="鄂尔多斯装货上报", notes="鄂尔多斯装货上报")
//	@PutMapping(value = "/loadingupload")
//	public Result<?> loadingupload(@RequestBody HyShippingNote hyshippingnote) {
//		return this.ordosReportService.reportShippingNote(hyshippingnote, ShippingStatus.运输中.getValue(), "114.5054000", "36.8635940");
//	}
//
//	@AutoLog(value = "鄂尔多斯卸货上报")
//	@ApiOperation(value="鄂尔多斯卸货上报", notes="鄂尔多斯卸货上报")
//	@PutMapping(value = "/unloadingupload")
//	public Result<?> unloadingupload(@RequestBody HyShippingNote hyshippingnote) {
//		return this.ordosReportService.reportShippingNote(hyshippingnote, ShippingStatus.已交货.getValue(), "114.5231660", "37.0823430");
//	}

	@ApiOperation(value="运税宝上报状态信息-分页列表查询", notes="梵运上报状态信息-分页列表查询")
	@GetMapping(value = "/ysbpdatalist")
	public Result<?> queryYunShuiBaoUpdataPageList(YunFuTongShippingNoteDTO yunFuTongShippingNoteDTO,
											   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		QueryWrapper<YunFuTongShippingNoteDTO> queryWrapper = new QueryWrapper();

		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");

		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", yunFuTongShippingNoteDTO.getCompanyId());
		}

		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",yunFuTongShippingNoteDTO.getCustomerId());
		}

		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getOrderId())){
			queryWrapper.eq("shipping.order_id",yunFuTongShippingNoteDTO.getOrderId());
		}

		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",yunFuTongShippingNoteDTO.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", yunFuTongShippingNoteDTO.getDriverName());
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getTelephone()) ) {
			queryWrapper.like("driver.telephone", yunFuTongShippingNoteDTO.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", yunFuTongShippingNoteDTO.getVehicleNumber());
		}

		//运单上报状态
		if (yunFuTongShippingNoteDTO.getDaodaUploadStatus().equals("0")){
			queryWrapper.ne("shipping.daoda_upload_status","1").or().isNull("shipping.daoda_upload_status");
			queryWrapper.nested(qw -> qw.ne("xlyect.upload_status", "1")
					.or().isNull("xlyect.upload_status")
			);
		}else {
			queryWrapper.eq("shipping.daoda_upload_status","1");
		}


		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",yunFuTongShippingNoteDTO.getPayDt_begin()+" 00:00:00");
		}


		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",yunFuTongShippingNoteDTO.getPayDt_end()+" 23:59:59" );
		}
		//创建时间
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getCreateTime_begin())){
			queryWrapper.ge("shipping.pay_dt",yunFuTongShippingNoteDTO.getCreateTime_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getCreateTime_end() )){
			queryWrapper.le("shipping.pay_dt",yunFuTongShippingNoteDTO.getCreateTime_end()+" 23:59:59" );
		}
		//发货时间
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtLoading_begin())){
			queryWrapper.ge("shipping.status_dt_loading",yunFuTongShippingNoteDTO.getStatusDtLoading_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtLoading_end() )){
			queryWrapper.le("shipping.status_dt_loading",yunFuTongShippingNoteDTO.getStatusDtLoading_end()+" 23:59:59" );
		}
		//卸货时间
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtUnloading_begin())){
			queryWrapper.ge("shipping.status_dt_unloading",yunFuTongShippingNoteDTO.getStatusDtUnloading_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtUnloading_end() )){
			queryWrapper.le("shipping.status_dt_unloading",yunFuTongShippingNoteDTO.getStatusDtUnloading_end()+" 23:59:59" );
		}
		//接单时间
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtReceive_begin())){
			queryWrapper.ge("shipping.status_dt_receive",yunFuTongShippingNoteDTO.getStatusDtReceive_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtReceive_end() )){
			queryWrapper.le("shipping.status_dt_receive",yunFuTongShippingNoteDTO.getStatusDtReceive_end()+" 23:59:59" );
		}
		//签收时间
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtSign_begin())){
			queryWrapper.ge("shipping.status_dt_sign",yunFuTongShippingNoteDTO.getStatusDtSign_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(yunFuTongShippingNoteDTO.getStatusDtSign_end() )){
			queryWrapper.le("shipping.status_dt_sign",yunFuTongShippingNoteDTO.getStatusDtSign_end()+" 23:59:59" );
		}

		//queryWrapper.orderByDesc("bill.pay_type","1");
		queryWrapper.groupBy("shipping.id");

		queryWrapper.orderByDesc("shipping.pay_dt");


		Page<YunFuTongShippingNoteDTO> page = new Page<>(pageNo, pageSize);
		IPage<YunFuTongShippingNoteDTO> pageList = yunfutongServiceAPI2.getYunShuiBaoPageList(page, queryWrapper);

//		对pageList使用迭代器进行迭代

		Iterator<YunFuTongShippingNoteDTO> iterator = pageList.getRecords().iterator();
		while (iterator.hasNext()) {
			YunFuTongShippingNoteDTO dto = iterator.next();
			if (yunFuTongShippingNoteDTO.getIzDrivershipping()!=null&&yunFuTongShippingNoteDTO.getIzDrivershipping().equals("1")&&!dto.getDriverName().equals(dto.getCarrier())){
				iterator.remove();
			}else {
				if (yunFuTongShippingNoteDTO.getIzDrivershipping()!=null&&yunFuTongShippingNoteDTO.getIzDrivershipping().equals("2")&&dto.getDriverName().equals(dto.getCarrier())){
					iterator.remove();
				}
			}
//			HyXlyReport xlyReport = xlyMapper.getByShippingId(dto.getId());
//			if (xlyReport!=null&&xlyReport.getUploadStatus().equals("1")){
//				iterator.remove();
//			}

		}

		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (YunFuTongShippingNoteDTO dto : pageList.getRecords()) {
				dto.setBillNum(DataMaskingUtil.bankNumberMasking(dto.getBillNum()));
				dto.setTelephone(DataMaskingUtil.phoneMasking(dto.getTelephone()));
				dto.setSjsfzh(DataMaskingUtil.phoneMasking(dto.getSjsfzh()));

			}
		}
		return Result.OK(pageList);
	}

	@AutoLog(value = "运税宝代开推送数据")
	@GetMapping(value = "/startPushYunfutong")
	public Result<?> pushKaiYunfutong(HttpServletRequest req, @RequestParam(name = "shippingId") String shippingId) throws ParseException, IOException, InterruptedException {

		if (!StringUtils.isNotBlank(shippingId)) {
			return Result.error("运单号为空!");
		}

		HyShippingNote shippingNote = hyShippingNoteService.getById(shippingId);
		HyShippingNote hyShippingNote = new HyShippingNote();
		hyShippingNote.setId(shippingId);
		if (shippingNote == null) {
			return Result.error("运单为空!");
		}
		long time1 = shippingNote.getStatusDtLoading().getTime();
		long time2 = shippingNote.getStatusDtUnloading().getTime();
		// 计算毫秒数差
		long diff = Math.abs(time1 - time2);
		// 将毫秒数差转换为分钟数
		long minutes = diff / (60 * 1000);
		if (minutes < 5) {
			return Result.error("实际发货时间与实际收货时间不能低于5分钟");
		}

		HyDriver driver = hyDriverService.getById(shippingNote.getDriverId());
		if (driver == null) {
			return Result.error("司机为空!");
		}
		yunfutongServiceAPI2.checkDriverInfo(driver);
		HyVehicle vehicle = hyVehicleService.getById(shippingNote.getVehicleId());
		if (vehicle == null) {
			return Result.error("车辆为空!");
		}
		yunfutongServiceAPI2.checkVehicleInfo(vehicle);
		HyCarrier carrier = hyCarrierService.getById(shippingNote.getCarrierId());

		HyShippingNoteBill shippingNoteBill = hyShippingNoteBillService.getBillByShippingId(shippingNote.getId());
		if (shippingNoteBill == null) {
			return Result.error("流水为空!");
		}
		//2、接口调用顺序：1)请求登录，获取token  2)文件上传 3)新增车辆 4)新增司机 5)新增运单(上传运单时 需要上传 银行回单、合同、运输凭证)
		//登录
		//Result<?> result = yunfutongServiceAPI.login();
		Result<?> result = yunfutongServiceAPI2.login(shippingNote.getCompanyId());
		if (!result.isSuccess()) {
			return Result.error(result.getMessage());
		}
		String token = result.getResult().toString();

		//查询司机是否存在货主名下
		/*Result<?> DriverIsExistResult = yunfutongServiceAPI2.IsExist(driver.getIdcard(), token, null);

		if (!DriverIsExistResult.isSuccess()) {
			return Result.error(DriverIsExistResult.getMessage());
		}
		log.info(DriverIsExistResult.getResult().toString());
		String driverResult = DriverIsExistResult.getResult().toString();
		Boolean DriverIsExist = Boolean.parseBoolean(driverResult);
		log.info("司机是否存在货主名下结果是========"+DriverIsExist);
		if (DriverIsExist==false){*/
		if (!yunfutongServiceAPI2.IsExist(driver.getIdcard(), token, null,shippingNote.getCompanyId())){
			//查询司机是否存在系统中
			Result<?> DriverIsExistResult2 = yunfutongServiceAPI2.checkDriver(driver.getIdcard(), token, driver.getTelephone(),shippingNote.getCompanyId());
			log.info("查询司机是否存在系统中结果为===="+DriverIsExistResult2);
			if (!DriverIsExistResult2.isSuccess()){
				log.info("开始添加司机信息请求");
				try {
					//上传司机证件
					driver  = yunfutongServiceAPI2.uploadDriver(driver, token,shippingNote.getCompanyId());
				}catch (Exception e){
					hyShippingNote.setDaodaUploadStatus("2");
					hyShippingNote.setDaodaFailureReason(e.getMessage());
					hyShippingNoteService.updateById(hyShippingNote);
					return Result.error(e.getMessage());
				}

				//添加司机到草稿箱中
				Result<?> result1 = yunfutongServiceAPI2.addDriver(driver, token, req, vehicle,shippingNote.getCompanyId());
				if (!result1.isSuccess() && !result1.getMessage().equals("已存在该手机号")) {
					hyShippingNote.setDaodaUploadStatus("2");
					hyShippingNote.setDaodaFailureReason(result1.getMessage());
					hyShippingNoteService.updateById(hyShippingNote);
					return Result.error(result1.getMessage());
				}
			}else {
				log.info("开始导入司机信息请求");
				try {
					//上传司机证件
					driver  = yunfutongServiceAPI2.uploadDriver(driver, token,shippingNote.getCompanyId());
				}catch (Exception e){
					hyShippingNote.setDaodaUploadStatus("2");
					hyShippingNote.setDaodaFailureReason(e.getMessage());
					hyShippingNoteService.updateById(hyShippingNote);
					return Result.error(e.getMessage());
				}

				//导入司机
				Result<?> result1 = yunfutongServiceAPI2.importDriver(driver, token, req, vehicle,shippingNote.getCompanyId());
				if (!result1.isSuccess() && !result1.getMessage().equals("已存在该手机号")) {
					Result<?> result2 = yunfutongServiceAPI2.addDriver(driver, token, req, vehicle,shippingNote.getCompanyId());
					if (!result2.isSuccess()) {
						hyShippingNote.setDaodaUploadStatus("2");
						hyShippingNote.setDaodaFailureReason(result2.getMessage());
						hyShippingNoteService.updateById(hyShippingNote);
						return Result.error("添加司机到草稿箱失败,原因为"+result2.getMessage());
					}
				}
			}
		}
		//查询车辆是否存在
		/*Result<?> hyVehicleIsExistResult = yunfutongServiceAPI2.IsExist(null, token, vehicle.getVehicleNumber());

		if (!hyVehicleIsExistResult.isSuccess()) {
			return Result.error(hyVehicleIsExistResult.getMessage());
		}
		String vehicleResult = hyVehicleIsExistResult.getResult().toString();
		Boolean hyVehicleIsExist = Boolean.parseBoolean(vehicleResult);
		log.info(hyVehicleIsExist.toString());
		if (!hyVehicleIsExist) {*/
		if (!yunfutongServiceAPI2.IsExist(null, token, vehicle.getVehicleNumber(),shippingNote.getCompanyId())) {
			HyVehicle hyVehicle = new HyVehicle();
			try {
				//上传车辆证件
				hyVehicle = yunfutongServiceAPI2.uploadVehicle(vehicle, token,shippingNote.getCompanyId());
			}catch (Exception e){
				hyShippingNote.setDaodaUploadStatus("2");
				hyShippingNote.setDaodaFailureReason(e.getMessage());
				hyShippingNoteService.updateById(hyShippingNote);
				return Result.error(e.getMessage());
			}

			Result<?> result2 = yunfutongServiceAPI2.addVehicle(hyVehicle, driver, token,shippingNote.getCompanyId());

			if (!result.isSuccess()) {
				hyShippingNote.setDaodaUploadStatus("2");
				hyShippingNote.setDaodaFailureReason(result2.getMessage());
				hyShippingNoteService.updateById(hyShippingNote);
				return Result.error(result2.getMessage());
			}
		}

		//上传运单凭证
		HyShippingNote shippingNoteUpload = yunfutongServiceAPI2.shippingNoteUpload(token, shippingNote);
		HyShippingNoteBill hyShippingNoteBill = yunfutongServiceAPI2.ShippingNoteBillUpload(token, shippingNoteBill);

		//添加运单
		Result<?> result3 = yunfutongServiceAPI2.addShippingNote(shippingNoteUpload, driver, vehicle, token, hyShippingNoteBill,carrier);
		log.info(result3.toString());
		hyShippingNote.setId(shippingId);

		if (!result3.isSuccess()) {
			hyShippingNote.setDaodaUploadStatus("2");
			hyShippingNote.setDaodaFailureReason(result3.getMessage());
			hyShippingNoteService.updateById(hyShippingNote);
			return Result.error(result3.getMessage());
		}
		if (shippingNote.getTosIdentification().equals("1")){
			log.info("共创旧数据上传");
			//上传轨迹
			Result<?> result4 = yunfutongServiceAPI2.uploadTraces(shippingNoteUpload, vehicle,token,shippingNote.getCompanyId());
			hyShippingNote.setId(shippingId);
			if (!result4.isSuccess()) {
				hyShippingNote.setDaodaUploadStatus("1");
				hyShippingNote.setDaodaFailureReason(result4.getMessage());
				hyShippingNoteService.updateById(hyShippingNote);
			} else {
				hyShippingNote.setDaodaUploadStatus("1");
				hyShippingNote.setDaodaFailureReason(result4.getMessage());
				hyShippingNote.setDaodaUploadTime(DateUtils.getDate());
				hyShippingNoteService.updateById(hyShippingNote);
			}
		}else {
			//上传轨迹
			Result<?> result4 = yunfutongServiceAPI2.uploadTraces(shippingNoteUpload, vehicle,token,shippingNote.getCompanyId());
			hyShippingNote.setId(shippingId);

			if (!result4.isSuccess()) {
				hyShippingNote.setDaodaUploadStatus("2");
				hyShippingNote.setDaodaFailureReason(result4.getMessage());
				hyShippingNoteService.updateById(hyShippingNote);
				return Result.error(result4.getMessage());
			} else {
				hyShippingNote.setDaodaUploadStatus("1");
				hyShippingNote.setDaodaFailureReason(result4.getMessage());
				hyShippingNote.setDaodaUploadTime(DateUtils.getDate());
				hyShippingNoteService.updateById(hyShippingNote);
			}
		}

		return Result.OK();
	}

	@AutoLog(value = "运税宝代开批量推送数据")
	@PostMapping(value = "/startPushYsbPl")
	public Result<?> startPushYsbPl(HttpServletRequest req, @RequestParam(name = "ids", required = true) String ids) throws ParseException, IOException {

		if (StringUtils.isBlank(ids)) {
			return Result.error("ids不能为空!");
		}
		List<HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
		HyShippingNote hyShippingNote1 = new HyShippingNote();
		HyShippingNote hyShippingNote2 = hyShippingNoteService.getById(ids.split(",")[0]);
		int num = 0;
		Result<?> result = yunfutongServiceAPI2.login(hyShippingNote2.getCompanyId());
		if (!result.isSuccess()) {
			return Result.error(result.getMessage());
		}
		String token = result.getResult().toString();
		for (HyShippingNote hyShippingNote : hyShippingNotes) {
			try {
				if (hyShippingNote.getStatusDtLoading() == null || hyShippingNote.getStatusDtUnloading() == null) {
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "装卸货时间为空!")
							.update();
					continue;
				}
				long time1 = hyShippingNote.getStatusDtLoading().getTime();
				long time2 = hyShippingNote.getStatusDtUnloading().getTime();
				// 计算毫秒数差
				long diff = Math.abs(time1 - time2);
				// 将毫秒数差转换为分钟数
				long minutes = diff / (60 * 1000);
				if (minutes < 5) {
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"实际装货时间与实际发货时间不能低于5分钟");
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "实际装货时间与实际发货时间不能低于5分钟!")
							.update();
					continue;
				}
				HyOrder order = hyOrderService.getById(hyShippingNote.getOrderId());
				if (order == null) {
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"货源为空!");
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "货源为空!")
							.update();
					continue;
				}
				HyDriver driver = hyDriverService.getById(hyShippingNote.getDriverId());
				if (driver == null) {
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"司机为空!");
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "货源为空!")
							.update();
					continue;
				}
				HyVehicle vehicle = hyVehicleService.getById(hyShippingNote.getVehicleId());
				if (vehicle == null) {
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"车辆为空!");
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "车辆为空!")
							.update();
					continue;
				}
				HyCarrier carrier = null;
				if (hyShippingNote.getPayType().equals("2")){
					carrier = hyCarrierService.getById(hyShippingNote.getCarrierId());
					if (carrier == null) {
						log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"代收人为空!");
						hyShippingNoteService.lambdaUpdate()
								.eq(HyShippingNote::getId,hyShippingNote.getId())
								.set(HyShippingNote::getDaodaUploadStatus, "2")
								.set(HyShippingNote::getDaodaFailureReason, "代收人为空!")
								.update();
						continue;
					}
				}
				HyShippingNoteBill shippingNoteBill = hyShippingNoteBillService.getBillByShippingId(hyShippingNote.getId());
				if (shippingNoteBill == null) {
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"流水为空!");
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, "流水为空!")
							.update();
					continue;
				}

				//2、接口调用顺序：1)请求登录，获取token  2)文件上传 3)新增车辆 4)新增司机 5)新增运单(上传运单时 需要上传 银行回单、合同、运输凭证)
				//查询司机是否存在
				/*Result<?> DriverIsExistResult = yunfutongServiceAPI2.IsExist(driver.getIdcard(), token, null);

				if (!DriverIsExistResult.isSuccess()) {
					//return Result.error(DriverIsExistResult.getMessage());
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"请求代开查询司机失败，原因为"+DriverIsExistResult.getMessage());
				}
				log.info(DriverIsExistResult.getResult().toString());
				String driverResult = DriverIsExistResult.getResult().toString();

				Boolean DriverIsExist = Boolean.parseBoolean(driverResult);
				if (!DriverIsExist) {*/
				if (!yunfutongServiceAPI2.IsExist(driver.getIdcard(), token, null,hyShippingNote.getCompanyId())) {
					//上传司机证件
					HyDriver uploadDriver = yunfutongServiceAPI2.uploadDriver(driver, token,hyShippingNote.getCompanyId());
					//添加司机
					Result<?> result1 = yunfutongServiceAPI2.addDriver(uploadDriver, token, req, vehicle,hyShippingNote.getCompanyId());
					if (!result1.isSuccess() && !result1.getMessage().equals("已存在该手机号")) {
						Result<?> addDriverResult = yunfutongServiceAPI2.addDriver(driver, token, req, vehicle,hyShippingNote.getCompanyId());
						if (!addDriverResult.isSuccess()) {
							/*hyShippingNote.setDaodaUploadStatus("2");
							hyShippingNote.setDaodaFailureReason(addDriverResult.getMessage());
							hyShippingNoteService.updateById(hyShippingNote);*/
							hyShippingNoteService.lambdaUpdate()
									.eq(HyShippingNote::getId,hyShippingNote.getId())
									.set(HyShippingNote::getDaodaUploadStatus, "2")
									.set(HyShippingNote::getDaodaFailureReason, result1.getMessage())
									.update();
							log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"添加司机到草稿箱失败,原因为"+addDriverResult.getMessage());
							continue;
						}
					}
				}
				//查询车辆是否存在
				/*Result<?> hyVehicleIsExistResult = yunfutongServiceAPI2.IsExist(null, token, vehicle.getVehicleNumber());

				if (!hyVehicleIsExistResult.isSuccess()) {
					return Result.error(hyVehicleIsExistResult.getMessage());
				}
				String vehicleResult = hyVehicleIsExistResult.getResult().toString();
				Boolean hyVehicleIsExist = Boolean.parseBoolean(vehicleResult);
				log.info(hyVehicleIsExist.toString());
				if (!hyVehicleIsExist) {*/
				if (!yunfutongServiceAPI2.IsExist(null, token, vehicle.getVehicleNumber(),hyShippingNote.getCompanyId())) {
					//上传车辆证件
					HyVehicle hyVehicle = yunfutongServiceAPI2.uploadVehicle(vehicle, token,hyShippingNote.getCompanyId());
					Result<?> result2 = yunfutongServiceAPI2.addVehicle(hyVehicle, driver, token,hyShippingNote.getCompanyId());
					if (!result.isSuccess()) {
						/*hyShippingNote1.setId(hyShippingNote.getId());
						hyShippingNote1.setDaodaUploadStatus("2");
						hyShippingNote1.setDaodaFailureReason(result2.getMessage());
						hyShippingNoteService.updateById(hyShippingNote1);*/
						hyShippingNoteService.lambdaUpdate()
								.eq(HyShippingNote::getId,hyShippingNote.getId())
								.set(HyShippingNote::getDaodaUploadStatus, "2")
								.set(HyShippingNote::getDaodaFailureReason, result2.getMessage())
								.update();
						log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"请求代开添加车辆失败，原因为"+result2.getMessage());
						continue;
					}
				}

				//上传运单凭证
				HyShippingNote shippingNoteUpload = yunfutongServiceAPI2.shippingNoteUpload(token, hyShippingNote);
				HyShippingNoteBill hyShippingNoteBill = yunfutongServiceAPI2.ShippingNoteBillUpload(token,shippingNoteBill);

				//添加运单
				Result<?> result3 = yunfutongServiceAPI2.addShippingNote(shippingNoteUpload, driver, vehicle, token, hyShippingNoteBill,carrier);
				log.info(result3.toString());
				hyShippingNote1.setId(hyShippingNote.getId());
				if (!result3.isSuccess()) {
					/*hyShippingNote1.setDaodaUploadStatus("2");
					hyShippingNote1.setDaodaFailureReason(result3.getMessage());
					hyShippingNoteService.updateById(hyShippingNote1);*/
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, result3.getMessage())
							.update();
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"请求代开添加运单失败，原因为"+result3.getMessage());
					continue;
				}
				Result<?> result4 = yunfutongServiceAPI2.uploadTraces(shippingNoteUpload, vehicle,token,hyShippingNote.getCompanyId());
				//hyShippingNote1.setId(hyShippingNote.getId());
				if (!result4.isSuccess()) {
					/*hyShippingNote1.setDaodaUploadStatus("2");
					hyShippingNote1.setDaodaFailureReason(result4.getMessage());
					hyShippingNoteService.updateById(hyShippingNote1);*/
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "2")
							.set(HyShippingNote::getDaodaFailureReason, result4.getMessage());
					log.error("运单号为"+hyShippingNote.getShippingNoteNumber()+"请求代开添加运单失败，原因为"+result4.getMessage());
				} else {
					/*hyShippingNote1.setDaodaUploadStatus("1");
					hyShippingNote1.setDaodaFailureReason(result4.getMessage());
					hyShippingNote1.setDaodaUploadTime(DateUtils.getDate());
					hyShippingNoteService.updateById(hyShippingNote1);*/
					hyShippingNoteService.lambdaUpdate()
							.eq(HyShippingNote::getId,hyShippingNote.getId())
							.set(HyShippingNote::getDaodaUploadStatus, "1")
							.set(HyShippingNote::getDaodaFailureReason, result4.getMessage());
				}
				num++;
			}catch (Exception e){
				log.error("运单号为："+ hyShippingNote.getShippingNoteNumber()+"的运单推送失败,原因为"+e.getMessage());
				continue;
			}

		}
		if (hyShippingNotes.size()!=num){
			return Result.error("部分运单推送失败,请查看运单状态");
		}
		return Result.OK();

	}
	/*****************视博etc***********************/
	@AutoLog(value = "视博etc")
	@RequestMapping(value = "/shiboEtcReport",method = RequestMethod.GET)
	@Transactional
	public Result<?> shiboEtcReport(@RequestParam("shippingNodeId")String shippingNodeId){
		HyShippingNote shippingNote = hyShippingNoteService.getById(shippingNodeId);
		HyVehicle vehicle = hyVehicleService.getById(shippingNote.getVehicleId());
		HyDriver driver = hyDriverService.getDriverById(shippingNote.getDriverId());
		try {

			String company3DESKey = null;
			Boolean result = true;
			boolean hasKey = redisUtil.hasKey("shibo3des");
			if (hasKey==true){
				company3DESKey = redisUtil.get("shibo3des").toString();
			}

			if (company3DESKey!=null){
				//确认密钥是否可用
				result = shiBoEtcService.doCompanyDesAck(company3DESKey);
			}
            if (company3DESKey==null||result==false){
				//获取3ds密钥
                company3DESKey = shiBoEtcService.getCompany3DESKey();
            }
            //上传车辆
            Result<?> veResult = shiBoEtcService.vehicleFiling(vehicle,company3DESKey);
			//如果失败则返回错误信息
			String time = DateUtil.getTime();
			if (!veResult.isSuccess()){
				updateShiboectReport(shippingNote, veResult);
				return Result.error(veResult.getMessage());
			}
			//对运单的签收时间跟现在的时间进行比较，如果大于3天则上传历史运单
			long day = DateUtil.getDaySub(DateUtil.getStringDate(shippingNote.getStatusDtLoading()),time);
			log.info("运单接单时间跟现在的时间相差"+day+"天");
			if (day>=3){
				Result<?> historyResult = shiBoEtcService.waybillHistoryStart(vehicle,shippingNote,company3DESKey);
				updateShiboectReport(shippingNote,historyResult);
				if (!historyResult.isSuccess()){
					//if (historyResult.getMessage().equals("运单编号重复")&&shiboectReportService.list(new LambdaQueryWrapper<>(ShiboEtcReport.class).eq(ShiboEtcReport::getShippingId,shippingNote.getId()).eq(ShiboEtcReport::getUploadStatus,"1")).size()!=0){
					if (historyResult.getMessage().contains("已存在")){
						historyResult.setSuccess(true);
						historyResult.setMessage("成功");
						updateShiboectReport(shippingNote,historyResult);
						return Result.OK();
					}
					return Result.error(historyResult.getMessage());
				}
				return Result.OK();
			}else {
				//开始上传运单
				Result<?> startResult = shiBoEtcService.waybillStart(shippingNote,vehicle,company3DESKey);
				if (!startResult.isSuccess()){
					updateShiboectReport(shippingNote,startResult);
					return Result.error(startResult.getMessage());
				}
				//上传原始运单
				Result<?> result2 = shiBoEtcService.originWaybillEnd(shippingNote,vehicle,driver,company3DESKey);
				if (!result2.isSuccess()){
					updateShiboectReport(shippingNote,result2);
					return Result.error(result2.getMessage());
				}
				//上传结束运单
				Result<?> endResult= shiBoEtcService.waybillEnd(shippingNote,company3DESKey);
				if (!endResult.isSuccess()){
					updateShiboectReport(shippingNote,endResult);
					return Result.error(endResult.getMessage());
				}

				if (startResult.isSuccess()&&result2.isSuccess()&&endResult.isSuccess()){
					updateShiboectReport(shippingNote,endResult);
					return Result.OK();
				}

			}
			// Result<?> result = shiBoEtcService.wayBillStatus(shippingNote,company3DESKey);
			//Result<?> result2 = shiBoEtcService.cancelWaybill(shippingNote,company3DESKey);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
		return Result.OK();
	}

	/**
	 * 根据运单编号撤销删除未上传成功的运单
	 * @param
	 * @param
	 */
	@GetMapping("/cancelWaybill")
	public Result<?> cancelWaybill(@RequestParam("shippingNodeId")String shippingNodeId){
		HyShippingNote shippingNote = hyShippingNoteService.getById(shippingNodeId);
		String company3DESKey = redisUtil.get("shibo3des").toString();
		ShiboEtcReport shiboEtcReport = shiboectReportService.getInfoByShippingId(shippingNote.getId());
		if (shiboEtcReport==null){
			return Result.error("该运单未上报不能撤销");
		}
		Result<?> result = shiBoEtcService.cancelWaybill(shippingNote,company3DESKey);
		if (result.isSuccess()){

			shiboEtcReport.setUploadStatus("0");
			shiboectReportService.updateById(shiboEtcReport);
			return Result.OK();
		}else {
			return Result.error(result.getMessage());
		}
	}

	/**
	 * 获取发票数据
	 * @param
	 * @param
	 */
	@PostMapping("/getInvoiceInfos")
	public Result<?> getInvoiceInfos(@RequestParam("shippingNoteNumber")String shippingNoteNumber,@RequestParam("clearDate") Date clearDate){
		String company3DESKey = redisUtil.get("shibo3des").toString();
		Result<?> result = shiBoEtcService.invoiceInfos(shippingNoteNumber,clearDate,company3DESKey);
		if (!result.isSuccess()){
			return Result.error(result.getMessage());
		}
		return Result.OK(result.getResult());



	}

	/**
	 *获取视博Etc列表
	 * @param
	 * @param
	 */
	@GetMapping("/getShiboEtcShippingNodeList")
	public Result<?> getShiboEtcShippingNodeList(ShiboEtcReportDTO shiboEtcReport,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		QueryWrapper<ShiboEtcReportDTO> queryWrapper = new QueryWrapper();
		log.info(shiboEtcReport.getUploadStatus());
		//运单签收之后就显示
		queryWrapper.eq("shipping.shipping_status", "5");
		if(StringUtils.isNotEmpty(shiboEtcReport.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", shiboEtcReport.getCompanyId());
		}

		if(StringUtils.isNotEmpty(shiboEtcReport.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",shiboEtcReport.getCustomerId());
		}

		if(StringUtils.isNotEmpty(shiboEtcReport.getOrderId())){
			queryWrapper.eq("shipping.order_id",shiboEtcReport.getOrderId());
		}

		if(StringUtils.isNotEmpty(shiboEtcReport.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",shiboEtcReport.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(shiboEtcReport.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", shiboEtcReport.getDriverName());
		}
		if(StringUtils.isNotEmpty(shiboEtcReport.getTelephone()) ) {
			queryWrapper.like("driver.telephone", shiboEtcReport.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(shiboEtcReport.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", shiboEtcReport.getVehicleNumber());
		}

		//针对安逸数据进行改造，其他用户如果用则放开
		//queryWrapper.eq("bill.bill_type","1");


		if(StringUtils.isNotEmpty(shiboEtcReport.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",shiboEtcReport.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(shiboEtcReport.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",shiboEtcReport.getPayDt_end()+" 23:59:59" );
		}
		//针对安阳导入的旧数据不显示(0正常运单，1历史运单)
		queryWrapper.eq("shipping.tos_identification","0");
		queryWrapper.orderByDesc("shipping.pay_dt");
		IPage<ShiboEtcReportDTO> pageList;

		if (StringUtils.isNotEmpty(shiboEtcReport.getUploadStatus())) {
			if (shiboEtcReport.getUploadStatus().contains(",")) {
				queryWrapper.in("sbect.upload_status", Arrays.asList(shiboEtcReport.getUploadStatus().split(",")));
			} else if (shiboEtcReport.getUploadStatus().equals("0")) {
				queryWrapper.isNull("sbect.upload_status");
			} else {
				queryWrapper.eq("sbect.upload_status", shiboEtcReport.getUploadStatus());
			}
		}else {
			queryWrapper.nested(
					qw -> qw.eq("sbect.upload_status", "2")
							.or().isNull("sbect.upload_status")
							.or().eq("sbect.upload_status", "0")
			);


		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getCreateTime_begin())) {
			queryWrapper.ge("shipping.create_time", shiboEtcReport.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getCreateTime_end())) {
			queryWrapper.le("shipping.create_time", shiboEtcReport.getCreateTime_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtUnloading_begin())) {
			queryWrapper.ge("shipping.status_dt_unloading", shiboEtcReport.getStatusDtUnloading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtUnloading_end())) {
			queryWrapper.le("shipping.status_dt_unloading", shiboEtcReport.getStatusDtUnloading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtReceive_begin())) {
			queryWrapper.ge("shipping.status_dt_receive", shiboEtcReport.getStatusDtReceive_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtReceive_end())) {
			queryWrapper.le("shipping.status_dt_receive", shiboEtcReport.getStatusDtReceive_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtLoading_begin())) {
			queryWrapper.ge("shipping.status_dt_loading", shiboEtcReport.getStatusDtLoading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtLoading_end())) {
			queryWrapper.le("shipping.status_dt_loading", shiboEtcReport.getStatusDtLoading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtSign_begin())) {
			queryWrapper.ge("shipping.status_dt_sign", shiboEtcReport.getStatusDtSign_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getStatusDtSign_end())) {
			queryWrapper.le("shipping.status_dt_sign", shiboEtcReport.getStatusDtSign_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDt_begin())) {
			queryWrapper.ge("shipping.pay_dt", shiboEtcReport.getPayDt_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDt_end())) {
			queryWrapper.le("shipping.pay_dt", shiboEtcReport.getPayDt_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDtCheck_begin())) {
			queryWrapper.ge("shipping.pay_dt_check", shiboEtcReport.getPayDtCheck_begin());
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDtCheck_end())) {
			queryWrapper.le("shipping.pay_dt_check", shiboEtcReport.getPayDtCheck_end());
		}

		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDtSubmitAut_begin())) {
			queryWrapper.ge("shipping.pay_dt_submit_aut", shiboEtcReport.getPayDtSubmitAut_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shiboEtcReport.getPayDtSubmitAut_end())) {
			queryWrapper.le("shipping.pay_dt_submit_aut", shiboEtcReport.getPayDtSubmitAut_end() + " 23:59:59");
		}
		Page<ShiboEtcReportDTO> page = new Page<ShiboEtcReportDTO>(pageNo, pageSize);

		//pageList = shiboectReportService.getShiboEtcList(page, queryWrapper);
		pageList = shiboectReportService.getShiboEtcList1(page, queryWrapper);
		return Result.OK(pageList);

	}

	@AutoLog(value = "视博ETC批量推送数据")
	@PostMapping(value = "/startPushShiboEtcPl")
	public Result<?> startPushShiboEtcPl(@RequestParam(name = "ids", required = true) String ids){
		if (StringUtils.isBlank(ids)) {
			return Result.error("ids不能为空!");
		}
		List<HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
		int num = 0;
		for (HyShippingNote shippingNote : hyShippingNotes) {
			try {
				HyVehicle vehicle = hyVehicleService.getById(shippingNote.getVehicleId());
				HyDriver driver = hyDriverService.getDriverById(shippingNote.getDriverId());

				String company3DESKey = null;
				Boolean result = true;
				boolean hasKey = redisUtil.hasKey("shibo3des");
				if (hasKey==true){
					company3DESKey = redisUtil.get("shibo3des").toString();
				}

				if (company3DESKey!=null){
					//确认密钥是否可用
					result = shiBoEtcService.doCompanyDesAck(company3DESKey);
				}
				if (company3DESKey==null||result==false){
					//获取3ds密钥
					company3DESKey = shiBoEtcService.getCompany3DESKey();
				}
				//上传车辆
				Result<?> veResult = shiBoEtcService.vehicleFiling(vehicle,company3DESKey);
				//如果失败则返回错误信息
				String time = DateUtil.getTime();
				if (!veResult.isSuccess()){
					log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报车辆失败，失败原因为++"+veResult.getMessage());
					updateShiboectReport(shippingNote, veResult);
					continue;
				}
				//对运单的签收时间跟现在的时间进行比较，如果大于3天则上传历史运单
				long day = DateUtil.getDaySub(DateUtil.getStringDate(shippingNote.getStatusDtLoading()),time);
				if (day>=3){
					Result<?> HistoryResult = shiBoEtcService.waybillHistoryStart(vehicle,shippingNote,company3DESKey);
					updateShiboectReport(shippingNote,HistoryResult);
					if (!HistoryResult.isSuccess()){
						log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报历史运单失败，失败原因为++"+HistoryResult.getMessage());
						continue;
					}
					num++;
					sleep(500);
				}else {
					//开始上传运单
					Result<?> startResult = shiBoEtcService.waybillStart(shippingNote,vehicle,company3DESKey);
					if (!startResult.isSuccess()){
						updateShiboectReport(shippingNote,startResult);
						log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报运单失败，失败原因为++"+startResult.getMessage());
						continue;
					}
					//上传原始运单
					Result<?> result2 = shiBoEtcService.originWaybillEnd(shippingNote,vehicle,driver,company3DESKey);
					if (!result2.isSuccess()){
						updateShiboectReport(shippingNote,result2);
						log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报原始运单失败，失败原因为++"+result2.getMessage());
						continue;
					}
					//上传结束运单
					Result<?> endResult= shiBoEtcService.waybillEnd(shippingNote,company3DESKey);
					if (!endResult.isSuccess()){
						updateShiboectReport(shippingNote,endResult);
						log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报结束运单失败，失败原因为++"+endResult.getMessage());
						continue;
					}

					if (startResult.isSuccess()&&result2.isSuccess()&&endResult.isSuccess()){
						num++;
						updateShiboectReport(shippingNote,endResult);
					}
					sleep(500);
				}
				// Result<?> result = shiBoEtcService.wayBillStatus(shippingNote,company3DESKey);
				//Result<?> result2 = shiBoEtcService.cancelWaybill(shippingNote,company3DESKey);
			} catch (Exception e) {
				log.error("运单号为"+shippingNote.getShippingNoteNumber()+"上报失败，失败原因为++"+e.getMessage());
			}
		}
		if(num<hyShippingNotes.size()){
			return Result.error("运单部分推送失败，请查看运单失败原因");
		}
		return Result.OK();
	}


		//修改或新增视博etc与运单关联表
	private void updateShiboectReport(HyShippingNote shippingNote,Result<?> result){
		ShiboEtcReport shiboEtcReport = new ShiboEtcReport();
		shiboEtcReport.setShippingId(shippingNote.getId());
		shiboEtcReport.setShippingNoteNumber(shippingNote.getShippingNoteNumber());
		if (result.isSuccess()){
			shiboEtcReport.setUploadStatus("1");
		}else {
			shiboEtcReport.setUploadStatus("2");
		}
		shiboEtcReport.setUploadDate(DateUtils.getDate());
		shiboEtcReport.setCreateBy(UserUtil.getLoginUser().getUsername());
		shiboEtcReport.setFailureReason(result.getMessage());
		Integer count = shiboectReportService.getCount(shippingNote.getId());
		if (count==0){
			shiboectReportService.save(shiboEtcReport);
		}else {
			ShiboEtcReport info = shiboectReportService.getInfoByShippingId(shippingNote.getId());
			shiboEtcReport.setId(info.getId());
			shiboectReportService.updateById(shiboEtcReport);
		}

	}
	/**
	 * 视博导出
	 * @param request
	 * @param shippingNoteDTO
	 * @return
	 */
	@RequestMapping(value = "/shiboExportXls")
	public ModelAndView shiboExportXls(HttpServletRequest request, HyShippingNoteDTO shippingNoteDTO) {
		QueryWrapper<HyShippingNoteDTO> queryWrapper = new QueryWrapper();
		String shippingPayStatus = shippingNoteDTO.getShippingPayStatus();
		HyCustomer customer = null;
		HyCustomerConfig customerConfig = null;

		String role = "";// 1 货主
		if (userUtil.isSys()) {

			if (StringUtils.isNotEmpty(shippingNoteDTO.getCustomerId())) {
				queryWrapper.eq("hy_shipping_note.customer_id", shippingNoteDTO.getCustomerId());
			}
		} else {
			customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			customerConfig = customerConfigService.getByCustomerId(customer.getId());

			role = "1";
			if ("4".equals(shippingPayStatus) || "7".equals(shippingPayStatus)) {
				queryWrapper.eq("hy_shipping_note.customer_id", customer.getId());
			} else {
				HyCustomer finalCustomer = customer;
				queryWrapper.and(
						qw -> qw.eq("hy_shipping_note.customer_id", finalCustomer.getId())
								.or()
								.eq("hy_shipping_note.agent_id", finalCustomer.getId()));
			}
		}

		//如果用货源号模糊搜索，而不是直接用货源ID进行搜索时
		if (StrUtil.isNotEmpty(shippingNoteDTO.getOrderNum()) && StrUtil.isEmpty(shippingNoteDTO.getOrderId())) {
			List<HyOrder> hyOrderList = this.hyOrderService.list(new LambdaQueryWrapper<HyOrder>().like(HyOrder::getOrderNum, shippingNoteDTO.getOrderNum()));
			List<String> orderIds = hyOrderList.stream().map(HyOrder::getId).collect(Collectors.toList());
			queryWrapper.in("hy_shipping_note.order_id", orderIds);
		}

		setShiboReportQueryWrapper(shippingNoteDTO, queryWrapper);

		queryWrapper.eq("hy_shipping_note.del_flag", "0");

		if ("TJSWYSB".equals(shippingNoteDTO.getExportMark())) {
			if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatusType())) {
				queryWrapper.nested(
						qw -> qw.eq("d.tj_tax_upload_status", "1")
								.eq("company.tj_tax_upload_status", "1")
								.eq("hy_shipping_note.tax_upload_status", "1")
								.eq("customer.tj_tax_upload_status", "1")
				);

			} else {

				queryWrapper.nested(
						qw -> qw.ne("d.tj_tax_upload_status", "1")
								.or().ne("company.tj_tax_upload_status", "1")
								.or().ne("hy_shipping_note.tax_upload_status", "1")
								.or().ne("customer.tj_tax_upload_status", "1")
				);
			}
			//导出
			if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_begin())) {
				queryWrapper.ge("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_begin() + " 00:00:00");
			} else {

				Date now1 = new Date();
				int month = now1.getMonth() + 1;
				if (month == 1) {
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(now1);
					calendar.add(Calendar.MONTH, -1);// 把日期往后增加一天.整数往后推,负数往前移动
					// 获取某月最小天数
					int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
					// 设置日历中月份的最小天数
					calendar.set(Calendar.DAY_OF_MONTH, firstDay);

					Date endDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
					String yearStartTime = DateUtils.date2Str(endDate, DateUtils.date_sdf.get()) + " 00:00:00";
					queryWrapper.ge("hy_shipping_note.pay_dt", yearStartTime);
				} else {
					Long start = System.currentTimeMillis();
					String yearStartTime = DateUtils.formatDate1(DateUtils.getYearStartTime(start));
					queryWrapper.ge("hy_shipping_note.pay_dt", yearStartTime);
				}

			}
		}


		String invoiceId = shippingNoteDTO.getInvoiceId();
		if (StringUtils.isNotEmpty(invoiceId)) {
			queryWrapper.eq("invoice_id", invoiceId);
		}
		queryWrapper.groupBy("hy_shipping_note.shipping_note_number");
		String byDesc = shippingNoteDTO.getByDesc();
		if (StringUtils.isNotEmpty(byDesc)) {
			String[] bys = byDesc.split(",");
			for (String by : bys) {
				queryWrapper.orderByDesc("hy_shipping_note." + by);
			}

		} else {
			queryWrapper.orderByDesc("hy_shipping_note.create_time");
		}

		String selections = request.getParameter("selections");
		if (StringUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("hy_shipping_note.id", selectionList);
		}

		String domianURL = request.getParameter("domianURL");
		if (StringUtils.isNotEmpty(domianURL)) {
			domianURL += "/";
		} else {
			domianURL = "";
		}

		List<shiboHyShippingNoteExportXlsDTO> list = shiboectReportService.getshiboExportXlsList(queryWrapper);
		for (shiboHyShippingNoteExportXlsDTO hyShippingNoteDTO : list) {
//			if("3".equals(hyShippingNoteDTO.getPayType())){
//				if(hyShippingNoteDTO.getCostPayTotal() != null && hyShippingNoteDTO.getPreFee() != null) {
//					hyShippingNoteDTO.setCostPayTotal(hyShippingNoteDTO.getCostPayTotal().add(hyShippingNoteDTO.getPreFee()));
//				}
//			}
			hyShippingNoteDTO.setContractUnitPrice(hyShippingNoteDTO.getShippingUnitPrice());
			hyShippingNoteDTO.setCostPayTotal(hyShippingNoteDTO.getCustomerFee());

//            if (hyShippingNoteDTO.getDriverBankId()==null||StringUtils.isEmpty(hyShippingNoteDTO.getDriverBankId())||hyShippingNoteDTO.getDriverBankId().equals("")){
//                //查找司机默认银行卡
//                HyDriverBank defaultDriverBank = hyDriverBankService.getDefaultDriverBank(hyShippingNoteDTO.getDriverId());
//
//                if (defaultDriverBank!=null){
//                    hyShippingNoteDTO.setSjyhkh(defaultDriverBank.getBankNum());
//                    hyShippingNoteDTO.setSjyhmc(defaultDriverBank.getBankType());
//                }
//            }
			String shippingContract = hyShippingNoteDTO.getShippingContract();
			if (StringUtils.isNotEmpty(shippingContract)) {
				hyShippingNoteDTO.setShippingContract(domianURL + shippingContract);
			}
			String imgLoading = hyShippingNoteDTO.getImgLoading();
			if (StringUtils.isNotEmpty(imgLoading)) {
				hyShippingNoteDTO.setImgLoading(domianURL + imgLoading);
			}

			String imgUnloading = hyShippingNoteDTO.getImgUnloading();
			if (StringUtils.isNotEmpty(imgUnloading)) {
				hyShippingNoteDTO.setImgUnloading(domianURL + imgUnloading);
			}

			if ("1".equals(role)) {
				if (customerConfig != null) {
					if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
						if (hyShippingNoteDTO.getOrderMode().equals("3")) {
							hyShippingNoteDTO.setCostService(new BigDecimal(0));
							hyShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyShippingNoteDTO.setGoodsWeightLoading(new BigDecimal(0));
							hyShippingNoteDTO.setGoodsWeightUnloading(new BigDecimal(0));
							hyShippingNoteDTO.setShippingUnitPrice(new BigDecimal(0));
							hyShippingNoteDTO.setContractUnitPrice(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay(new BigDecimal(0));
							hyShippingNoteDTO.setPreUnitFee(new BigDecimal(0));
							hyShippingNoteDTO.setPreFee(new BigDecimal(0));
							hyShippingNoteDTO.setCostPayShipping(new BigDecimal(0));
							hyShippingNoteDTO.setDriverFee(new BigDecimal(0));
							hyShippingNoteDTO.setCostPayTotal(new BigDecimal(0));
							hyShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyShippingNoteDTO.setAgentCostPay(new BigDecimal(0));
							hyShippingNoteDTO.setAgentCostPayShipping(new BigDecimal(0));
							hyShippingNoteDTO.setGasRechargeAmount(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay1(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay2(new BigDecimal(0));

						}
					}
				}
			}
		}

		//如果导出的运单中有货主模式的运单，需要导出货主单价
		boolean orderMode = false;
		if (null != list.stream().filter(s -> s.getOrderMode().equals("4")).findAny().orElse(null)) {
			orderMode = true;
		}
		log.info("是否是货主模式导出,{}", orderMode);

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String title = "运单表";
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, shiboHyShippingNoteExportXlsDTO.class);

		//按选择列导出
		//log.info("数据-> {}", JSONUtil.toJsonStr(list));
		String selCols = request.getParameter("selCols");
		log.info("选择列导出运单-> cols {}", selCols);
		if ("1".equals(selCols)) {
			String field = request.getParameter("field");
			log.info("选择列导出运单-> field {}", field);
			if (StringUtils.isNotEmpty(field)) {
				List<String> fields = Arrays.asList(field.split(","));
				List<String> selectCols = new ArrayList<>();
				for (String s : fields) {
					if (StringUtils.isNotEmpty(s)) {
						int idx = s.indexOf("_dictText");
						if (idx > -1) {
							s = s.substring(0, idx);
						}
						selectCols.add(s);
					}
				}

				//运输路线导出，条件routeSlot
				if (fields.contains("routeSlot")) {
					selectCols.add("sendAddressName");
					selectCols.add("receiveAddressName");
				}

				//收发货区域导出,条件
				if (fields.contains("routeArea")) {
					selectCols.add("sendAreaCode");
					selectCols.add("receiveAreaCode");
				}

				if (orderMode) {
					selectCols.add("customerPrice");
					selectCols.add("customerUnitPrice");
				}

				mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(selectCols, ","));
			}
		}

		ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		exportParams.setType(ExcelType.XSSF);
		exportParams.setStyle(HyShippingExcelExportImpl.class);
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}

	private void setShiboReportQueryWrapper(HyShippingNoteDTO shippingNoteDTO, QueryWrapper<HyShippingNoteDTO> queryWrapper) {

		String shippingPayStatus = shippingNoteDTO.getShippingPayStatus();

		if (StringUtils.isNotEmpty(shippingPayStatus)) {
			if (shippingPayStatus.indexOf("!") == 0) {
				shippingPayStatus = shippingPayStatus.substring(1, shippingPayStatus.length());
				queryWrapper.ne("hy_shipping_note.shipping_pay_status", shippingPayStatus);
			} else {
				if (shippingPayStatus.contains(",")) {
					queryWrapper.in("hy_shipping_note.shipping_pay_status", Arrays.asList(shippingPayStatus.split(",")));
				} else {
					queryWrapper.eq("hy_shipping_note.shipping_pay_status", shippingPayStatus);
				}
			}
		}

		/*if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatus())) {
			if (shippingNoteDTO.getUploadStatus().contains(",")) {
				queryWrapper.in("xlyect.upload_status", Arrays.asList(shippingNoteDTO.getUploadStatus().split(",")));
			} else {
				queryWrapper.eq("xlyect.upload_status", shippingNoteDTO.getUploadStatus());
			}
		}else {
			queryWrapper.nested(
					qw -> qw.eq("xlyect.upload_status", "2")
							.or().eq("xlyect.upload_status","0")
							.or().isNull("xlyect.upload_status")
			);


		}*/
		if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatus())) {
			if (shippingNoteDTO.getUploadStatus().contains(",")) {
				queryWrapper.in("sbect.upload_status", Arrays.asList(shippingNoteDTO.getUploadStatus().split(",")));
			} else if (shippingNoteDTO.getUploadStatus().equals("0")) {
				queryWrapper.isNull("sbect.upload_status");
			} else {
				queryWrapper.eq("sbect.upload_status", shippingNoteDTO.getUploadStatus());
			}
		}else {
			queryWrapper.nested(
					qw -> qw.eq("sbect.upload_status", "2")
							.or().isNull("sbect.upload_status")
			);


		}
		/*if (shippingNoteDTO.getEtcUploadStatus()==null){
			queryWrapper.nested(
					qw -> qw.eq("shipping.ky_state", "2")
							.or().eq("shipping.ky_state","0")
							.or().isNull("shipping.ky_state")
			);
		}*/

		if (StringUtils.isNotEmpty(shippingNoteDTO.getSendAddress())) {
			queryWrapper.like("hy_shipping_note.send_address", shippingNoteDTO.getSendAddress());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getReceiveAddress())) {
			queryWrapper.like("hy_shipping_note.receive_address", shippingNoteDTO.getReceiveAddress());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getSendAddressName())) {
			queryWrapper.like("hy_shipping_note.send_address_name", shippingNoteDTO.getSendAddressName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getReceiveAddressName())) {
			queryWrapper.like("hy_shipping_note.receive_address_name", shippingNoteDTO.getReceiveAddressName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getKhdh())) {
			queryWrapper.like("hy_shipping_note.khdh", shippingNoteDTO.getKhdh());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractNum2())) {
			queryWrapper.eq("hy_shipping_note.contract_num2", shippingNoteDTO.getContractNum2());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractStatus())) {
			queryWrapper.eq("hy_shipping_note.contract_status", shippingNoteDTO.getContractStatus());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getOrderId())) {
			queryWrapper.eq("hy_shipping_note.order_id", shippingNoteDTO.getOrderId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("hy_shipping_note.company_id", shippingNoteDTO.getCompanyId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getShippingStatus())) {
			queryWrapper.eq("hy_shipping_note.shipping_status", shippingNoteDTO.getShippingStatus());
		}

		//Added start at 2024-04-10 for 筛选是否生成运输合同
		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractCreated())) {
			//0未生成，判断null和字符串空
			if ("0".equals(shippingNoteDTO.getContractCreated())) {
				queryWrapper.nested(
						qw->qw.isNull("hy_shipping_note.shipping_contract")
								.or().eq("hy_shipping_note.shipping_contract", ""));
			} else if ("1".equals(shippingNoteDTO.getContractCreated())) {
				queryWrapper.isNotNull("hy_shipping_note.shipping_contract");
				queryWrapper.ne("hy_shipping_note.shipping_contract", "");
			}

			queryWrapper.ne("hy_shipping_note.shipping_status", "8");
		}
		//Added end at 2024-04-10 for 筛选是否生成运输合同

		String orderMode = shippingNoteDTO.getOrderMode();
		if (StringUtils.isNotEmpty(orderMode)) {
			if (orderMode.contains(",")) {
				queryWrapper.in("hy_shipping_note.order_mode", Arrays.asList(orderMode.split(",")));
			} else {
				queryWrapper.eq("hy_shipping_note.order_mode", orderMode);
			}
		}
		String payChannel = shippingNoteDTO.getPayChannel();
		if (StringUtils.isNotEmpty(payChannel)) {
			if (payChannel.contains(",")) {
				queryWrapper.in("hy_shipping_note.pay_channel", Arrays.asList(payChannel.split(",")));
			} else {
				queryWrapper.eq("hy_shipping_note.pay_channel", shippingNoteDTO.getPayChannel());
			}
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getDaodaUploadStatus())) {
			queryWrapper.eq("hy_shipping_note.daoda_upload_status", shippingNoteDTO.getDaodaUploadStatus());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadNucarEtc())) {
			queryWrapper.eq("hy_shipping_note.upload_nucar_etc", shippingNoteDTO.getUploadNucarEtc());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getAlctIdentification())) {
			queryWrapper.eq("hy_shipping_note.alct_identification", shippingNoteDTO.getAlctIdentification());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTosIdentification())) {
			queryWrapper.eq("hy_shipping_note.tos_identification", shippingNoteDTO.getTosIdentification());
		}

		/*if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatus())) {
			queryWrapper.eq("hy_shipping_note.upload_status", shippingNoteDTO.getUploadStatus());
		}*/


		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillStatus())) {
			queryWrapper.in("hy_shipping_note.bill_status", Arrays.asList(shippingNoteDTO.getBillStatus().split(",")));
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getDriverName())) {
			queryWrapper.like("d.driver_name", shippingNoteDTO.getDriverName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTelephone())) {
			queryWrapper.like("d.telephone", shippingNoteDTO.getTelephone());
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getVehicleNumber())) {
			queryWrapper.like("v.vehicle_number", shippingNoteDTO.getVehicleNumber());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getShippingNoteNumber())) {
			queryWrapper.like("hy_shipping_note.shipping_note_number", shippingNoteDTO.getShippingNoteNumber());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getDescriptionOfGoods())) {
			queryWrapper.like("hy_shipping_note.description_of_goods", shippingNoteDTO.getDescriptionOfGoods());
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getCreateTime_begin())) {
			queryWrapper.ge("hy_shipping_note.create_time", shippingNoteDTO.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getCreateTime_end())) {
			queryWrapper.le("hy_shipping_note.create_time", shippingNoteDTO.getCreateTime_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtReceive_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_receive", shippingNoteDTO.getStatusDtReceive_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtReceive_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_receive", shippingNoteDTO.getStatusDtReceive_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtLoading_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_loading", shippingNoteDTO.getStatusDtLoading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtLoading_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_loading", shippingNoteDTO.getStatusDtLoading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtSign_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_sign", shippingNoteDTO.getStatusDtSign_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtSign_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_sign", shippingNoteDTO.getStatusDtSign_end() + " 23:59:59");
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getTaxUploadDate_begin())) {
			queryWrapper.ge("hy_shipping_note.tax_upload_date", shippingNoteDTO.getTaxUploadDate_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTaxUploadDate_end())) {
			queryWrapper.le("hy_shipping_note.tax_upload_date", shippingNoteDTO.getTaxUploadDate_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtCheck_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt_check", shippingNoteDTO.getPayDtCheck_begin());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtCheck_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt_check", shippingNoteDTO.getPayDtCheck_end());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtSubmitAut_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt_submit_aut", shippingNoteDTO.getPayDtSubmitAut_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtSubmitAut_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt_submit_aut", shippingNoteDTO.getPayDtSubmitAut_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getCarrierId())) {
			queryWrapper.eq("hy_shipping_note.carrier_id", shippingNoteDTO.getCarrierId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getIntroducer())) {
			queryWrapper.like("hy_shipping_note.introducer", shippingNoteDTO.getIntroducer());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getDispatcher())) {
			queryWrapper.like("hy_shipping_note.dispatcher", shippingNoteDTO.getDispatcher());
		}
		//预付
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPrePayStatus())) {
			queryWrapper.eq("hy_shipping_note.pre_pay_status", shippingNoteDTO.getPrePayStatus());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayType())) {
			queryWrapper.eq("hy_shipping_note.pay_type", shippingNoteDTO.getPayType());
		}

		//增加开票完成时间段查询
		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillDtComplateBegin())) {
			queryWrapper.ge("hy_shipping_note.bill_dt_complate", shippingNoteDTO.getBillDtComplateBegin());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillDtComplateEnd())) {
			queryWrapper.le("hy_shipping_note.bill_dt_complate", shippingNoteDTO.getBillDtComplateEnd());
		}

	}
/*****************************新陆运******************************************



 	/**
	 * 新陆运推送
	 */
	@AutoLog(value = "新陆运推送")
	@GetMapping("/startPushXly")
	@Transactional
	public Result<?> startPushXly(@RequestParam("shippingId") String shippingId){
		HyShippingNote shippingNote = hyShippingNoteService.getById(shippingId);
		if (shippingNote==null){
			return Result.error("运单不存在");
		}
		List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(new LambdaQueryWrapper<HyShippingNoteBill>().eq(HyShippingNoteBill::getShippingId, shippingNote.getId()));
		if (CollUtil.isEmpty(shippingNoteBills)) {
			return Result.error(shippingNote.getShippingNoteNumber() + " ,资金流水不存在");
		}
		HyVehicle vehicle = hyVehicleService.getById(shippingNote.getVehicleId());
		HyDriver driver = hyDriverService.getDriverById(shippingNote.getDriverId());
		Result<?> result = xlyService.upload(driver,vehicle,shippingNoteBills,shippingNote);
		return result;

	}

	/**
	 *
	 */
	@AutoLog(value = "新陆运批量推送数据")
	@PostMapping(value = "/startPushXlyboEtcPl")
	public Result<?> startPushXlyboEtcPl(@RequestParam(name = "ids", required = true) String ids){
		if (StringUtils.isBlank(ids)) {
			return Result.error("ids不能为空!");
		}

		List<HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIdsForUpdate(Arrays.asList(ids.split(",")));
		int uploadCount = 0;
		for (HyShippingNote shippingNote : hyShippingNotes) {
				HyVehicle vehicle = hyVehicleService.getById(shippingNote.getVehicleId());
				HyDriver driver = hyDriverService.getDriverById(shippingNote.getDriverId());
				List<HyShippingNoteBill> shippingNoteBills = hyShippingNoteBillService.list(new LambdaQueryWrapper<HyShippingNoteBill>().eq(HyShippingNoteBill::getShippingId, shippingNote.getId()));
				if (CollUtil.isEmpty(shippingNoteBills)) {
					return Result.error(shippingNote.getShippingNoteNumber() + " ,资金流水不存在");
				}
				try {
					Result<?> result = xlyService.upload(driver, vehicle, shippingNoteBills, shippingNote);
					log.info("河南信用平台代开批量上报，{},{},{}",shippingNote.getShippingNoteNumber(),result.getMessage());
					if (result.isSuccess()){
						uploadCount++;
					}
					sleep(500);
				} catch (InterruptedException e) {
					log.error("河南信用平台代开批量上报{} , {}", shippingNote.getShippingNoteNumber(), e.getMessage());
				}
			}
		log.info("uploadCount的长度"+uploadCount);
		log.info("hyShippingNotes.size()"+hyShippingNotes.size());
		if (uploadCount < hyShippingNotes.size()) {
			return Result.error("操作完成：部分运单上报失败，请查看运单上报状态!");
		}

		return Result.OK();
	}



	/**
	 * 获取列表
	 */
	@GetMapping("/xlydatalist")
	public Result<?> xlydatalist(XlyReportDTO xlyReport,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		QueryWrapper<XlyReportDTO> queryWrapper = new QueryWrapper();
		//运单
		queryWrapper.eq("shipping.shipping_status", "5");
		queryWrapper.eq("shipping.shipping_pay_status", "6");

		if(StringUtils.isNotEmpty(xlyReport.getCompanyId())) {
			queryWrapper.eq("shipping.company_id", xlyReport.getCompanyId());
		}

		if(StringUtils.isNotEmpty(xlyReport.getCustomerId())){
			queryWrapper.eq("shipping.customer_id",xlyReport.getCustomerId());
		}

		if(StringUtils.isNotEmpty(xlyReport.getOrderId())){
			queryWrapper.eq("shipping.order_id",xlyReport.getOrderId());
		}

		if(StringUtils.isNotEmpty(xlyReport.getShippingNoteNumber())){
			queryWrapper.like("shipping.shipping_note_number",xlyReport.getShippingNoteNumber());
		}

		//司机
		if(StringUtils.isNotEmpty(xlyReport.getDriverName()) ) {
			queryWrapper.like("driver.driver_name", xlyReport.getDriverName());
		}
		if(StringUtils.isNotEmpty(xlyReport.getTelephone()) ) {
			queryWrapper.like("driver.telephone", xlyReport.getTelephone());
		}

		//车辆
		if(StringUtils.isNotEmpty(xlyReport.getVehicleNumber())){
			queryWrapper.like("vehicle.vehicle_number", xlyReport.getVehicleNumber());
		}



		queryWrapper.eq("bill.bill_type","1");


		if(StringUtils.isNotEmpty(xlyReport.getPayDt_begin())){
			queryWrapper.ge("shipping.pay_dt",xlyReport.getPayDt_begin()+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(xlyReport.getPayDt_end() )){
			queryWrapper.le("shipping.pay_dt",xlyReport.getPayDt_end()+" 23:59:59" );
		}

		if (StringUtils.isNotEmpty(xlyReport.getEtcUploadStatus())) {
			if (xlyReport.getEtcUploadStatus().contains(",")) {
				queryWrapper.in("xlyect.upload_status", Arrays.asList(xlyReport.getEtcUploadStatus().split(",")));
			} else {
				queryWrapper.eq("xlyect.upload_status", xlyReport.getEtcUploadStatus());
			}
		}else {
			queryWrapper.nested(
					qw -> qw.eq("xlyect.upload_status", "2")
							.or().eq("xlyect.upload_status","0")
							.or().isNull("xlyect.upload_status")
			);
			//queryWrapper.ne("shipping.daoda_upload_status","1");


		}
		/*if (xlyReport.getEtcUploadStatus()==null){
			queryWrapper.nested(
					qw -> qw.eq("shipping.ky_state", "2")
							.or().eq("shipping.ky_state","0")
							.or().isNull("shipping.ky_state")
			);
		}*/
		if (StringUtils.isNotEmpty(xlyReport.getCreateTime_begin())) {
			queryWrapper.ge("shipping.create_time", xlyReport.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(xlyReport.getCreateTime_end())) {
			queryWrapper.le("shipping.create_time", xlyReport.getCreateTime_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(xlyReport.getStatusDtUnloading_begin())) {
			queryWrapper.ge("shipping.status_dt_unloading", xlyReport.getStatusDtUnloading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(xlyReport.getStatusDtUnloading_end())) {
			queryWrapper.le("shipping.status_dt_unloading", xlyReport.getStatusDtUnloading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(xlyReport.getStatusDtReceive_begin())) {
			queryWrapper.ge("shipping.status_dt_receive", xlyReport.getStatusDtReceive_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(xlyReport.getStatusDtReceive_end())) {
			queryWrapper.le("shipping.status_dt_receive", xlyReport.getStatusDtReceive_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(xlyReport.getStatusDtLoading_begin())) {
			queryWrapper.ge("shipping.status_dt_loading", xlyReport.getStatusDtLoading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(xlyReport.getStatusDtLoading_end())) {
			queryWrapper.le("shipping.status_dt_loading", xlyReport.getStatusDtLoading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(xlyReport.getStatusDtSign_begin())) {
			queryWrapper.ge("shipping.status_dt_sign", xlyReport.getStatusDtSign_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(xlyReport.getStatusDtSign_end())) {
			queryWrapper.le("shipping.status_dt_sign", xlyReport.getStatusDtSign_end() + " 23:59:59");
		}
		queryWrapper.orderByDesc("shipping.pay_dt");
		IPage<XlyReportDTO> pageList;
		Page<XlyReportDTO> page = new Page<XlyReportDTO>(pageNo, pageSize);
		pageList = xlyService.getXinluyunEtcList(page,queryWrapper);
		return Result.OK(pageList);

	}
	/**
	 * 删除运单
	 */
	@DeleteMapping("/remove")
	public Result<?> remove(@RequestParam(name = "id") String id){
		HyShippingNote shippingNote = hyShippingNoteService.getById(id);
		if (shippingNote==null){
			return Result.error("该运单不存在");
		}
		//登录获取token
		boolean hasKey = redisUtil.hasKey("WSAPIToken");
		String token = null;
		if (hasKey == true) {
			token = redisUtil.get("WSAPIToken").toString();
		} else {
			Result<?> LoginResult = wlxyptServiceApi.login();
			if (LoginResult.isSuccess()) {
				token = LoginResult.getResult().toString();
			} else {
				return Result.error(LoginResult.getMessage());
			}
		}
		try {
			Result<?> result = wlxyptServiceApi.deleteShippingNode(shippingNote.getShippingNoteNumber(), token);
			if (result.isSuccess()){
				xlyService.deleteByShippingId(shippingNote.getId());
				return Result.OK();
			}else {
				return Result.error(result.getMessage());
			}
		} catch (IOException e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	@AutoLog(value = "银行回单上传")
	@ApiOperation(value="银行回单上传", notes="银行回单上传")
	@PutMapping(value = "/uploadBankHD")
	public Result<?> uploadBankHD(@RequestBody HyShippingNoteHD hyShippingNote) {
		return this.xlyService.uploadBankHD(hyShippingNote);
	}


	/**
	 * 河南信用平台导出
	 * @param request
	 * @param shippingNoteDTO
	 * @return
	 */
	@RequestMapping(value = "/xlyExportXls")
	public ModelAndView xlyExportXls(HttpServletRequest request, HyShippingNoteXlyReportDTO shippingNoteDTO) {
		QueryWrapper<HyShippingNoteDTO> queryWrapper = new QueryWrapper();
		String shippingPayStatus = shippingNoteDTO.getShippingPayStatus();
		HyCustomer customer = null;
		HyCustomerConfig customerConfig = null;

		String role = "";// 1 货主
		if (userUtil.isSys()) {

			if (StringUtils.isNotEmpty(shippingNoteDTO.getCustomerId())) {
				queryWrapper.eq("hy_shipping_note.customer_id", shippingNoteDTO.getCustomerId());
			}
		} else {
			customer = userUtil.getCustomer();
			if (customer == null) {
				throw new JeecgBootException("货主信息不存在!");
			}
			customerConfig = customerConfigService.getByCustomerId(customer.getId());

			role = "1";
			if ("4".equals(shippingPayStatus) || "7".equals(shippingPayStatus)) {
				queryWrapper.eq("hy_shipping_note.customer_id", customer.getId());
			} else {
				HyCustomer finalCustomer = customer;
				queryWrapper.and(
						qw -> qw.eq("hy_shipping_note.customer_id", finalCustomer.getId())
								.or()
								.eq("hy_shipping_note.agent_id", finalCustomer.getId()));
			}
		}

		//如果用货源号模糊搜索，而不是直接用货源ID进行搜索时
		if (StrUtil.isNotEmpty(shippingNoteDTO.getOrderNum()) && StrUtil.isEmpty(shippingNoteDTO.getOrderId())) {
			List<HyOrder> hyOrderList = this.hyOrderService.list(new LambdaQueryWrapper<HyOrder>().like(HyOrder::getOrderNum, shippingNoteDTO.getOrderNum()));
			List<String> orderIds = hyOrderList.stream().map(HyOrder::getId).collect(Collectors.toList());
			queryWrapper.in("hy_shipping_note.order_id", orderIds);
		}

		setXlyReportQueryWrapper(shippingNoteDTO, queryWrapper);

		queryWrapper.eq("hy_shipping_note.del_flag", "0");

		if ("TJSWYSB".equals(shippingNoteDTO.getExportMark())) {
			if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatusType())) {
				queryWrapper.nested(
						qw -> qw.eq("d.tj_tax_upload_status", "1")
								.eq("company.tj_tax_upload_status", "1")
								.eq("hy_shipping_note.tax_upload_status", "1")
								.eq("customer.tj_tax_upload_status", "1")
				);

			} else {

				queryWrapper.nested(
						qw -> qw.ne("d.tj_tax_upload_status", "1")
								.or().ne("company.tj_tax_upload_status", "1")
								.or().ne("hy_shipping_note.tax_upload_status", "1")
								.or().ne("customer.tj_tax_upload_status", "1")
				);
			}
			//导出
			if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_begin())) {
				queryWrapper.ge("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_begin() + " 00:00:00");
			} else {

				Date now1 = new Date();
				int month = now1.getMonth() + 1;
				if (month == 1) {
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(now1);
					calendar.add(Calendar.MONTH, -1);// 把日期往后增加一天.整数往后推,负数往前移动
					// 获取某月最小天数
					int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
					// 设置日历中月份的最小天数
					calendar.set(Calendar.DAY_OF_MONTH, firstDay);

					Date endDate = calendar.getTime(); // 这个时间就是日期往后推一天的结果
					String yearStartTime = DateUtils.date2Str(endDate, DateUtils.date_sdf.get()) + " 00:00:00";
					queryWrapper.ge("hy_shipping_note.pay_dt", yearStartTime);
				} else {
					Long start = System.currentTimeMillis();
					String yearStartTime = DateUtils.formatDate1(DateUtils.getYearStartTime(start));
					queryWrapper.ge("hy_shipping_note.pay_dt", yearStartTime);
				}

			}
		}


		String invoiceId = shippingNoteDTO.getInvoiceId();
		if (StringUtils.isNotEmpty(invoiceId)) {
			queryWrapper.eq("invoice_id", invoiceId);
		}
		queryWrapper.groupBy("hy_shipping_note.shipping_note_number");
		String byDesc = shippingNoteDTO.getByDesc();
		if (StringUtils.isNotEmpty(byDesc)) {
			String[] bys = byDesc.split(",");
			for (String by : bys) {
				queryWrapper.orderByDesc("hy_shipping_note." + by);
			}

		} else {
			queryWrapper.orderByDesc("hy_shipping_note.create_time");
		}

		String selections = request.getParameter("selections");
		if (StringUtils.isNotEmpty(selections)) {
			List<String> selectionList = Arrays.asList(selections.split(","));
			queryWrapper.in("hy_shipping_note.id", selectionList);
		}

		String domianURL = request.getParameter("domianURL");
		if (StringUtils.isNotEmpty(domianURL)) {
			domianURL += "/";
		} else {
			domianURL = "";
		}

		List<HyShippingNoteDTO> list = xlyService.getXlyReportList(queryWrapper);
		for (HyShippingNoteDTO hyShippingNoteDTO : list) {
//			if("3".equals(hyShippingNoteDTO.getPayType())){
//				if(hyShippingNoteDTO.getCostPayTotal() != null && hyShippingNoteDTO.getPreFee() != null) {
//					hyShippingNoteDTO.setCostPayTotal(hyShippingNoteDTO.getCostPayTotal().add(hyShippingNoteDTO.getPreFee()));
//				}
//			}
			hyShippingNoteDTO.setContractUnitPrice(hyShippingNoteDTO.getShippingUnitPrice());
			hyShippingNoteDTO.setCostPayTotal(hyShippingNoteDTO.getCustomerFee());

//            if (hyShippingNoteDTO.getDriverBankId()==null||StringUtils.isEmpty(hyShippingNoteDTO.getDriverBankId())||hyShippingNoteDTO.getDriverBankId().equals("")){
//                //查找司机默认银行卡
//                HyDriverBank defaultDriverBank = hyDriverBankService.getDefaultDriverBank(hyShippingNoteDTO.getDriverId());
//
//                if (defaultDriverBank!=null){
//                    hyShippingNoteDTO.setSjyhkh(defaultDriverBank.getBankNum());
//                    hyShippingNoteDTO.setSjyhmc(defaultDriverBank.getBankType());
//                }
//            }
			String shippingContract = hyShippingNoteDTO.getShippingContract();
			if (StringUtils.isNotEmpty(shippingContract)) {
				hyShippingNoteDTO.setShippingContract(domianURL + shippingContract);
			}
			String imgLoading = hyShippingNoteDTO.getImgLoading();
			if (StringUtils.isNotEmpty(imgLoading)) {
				hyShippingNoteDTO.setImgLoading(domianURL + imgLoading);
			}

			String imgUnloading = hyShippingNoteDTO.getImgUnloading();
			if (StringUtils.isNotEmpty(imgUnloading)) {
				hyShippingNoteDTO.setImgUnloading(domianURL + imgUnloading);
			}

			if ("1".equals(role)) {
				if (customerConfig != null) {
					if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
						if (hyShippingNoteDTO.getOrderMode().equals("3")) {
							hyShippingNoteDTO.setCostService(new BigDecimal(0));
							hyShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyShippingNoteDTO.setGoodsWeightLoading(new BigDecimal(0));
							hyShippingNoteDTO.setGoodsWeightUnloading(new BigDecimal(0));
							hyShippingNoteDTO.setShippingUnitPrice(new BigDecimal(0));
							hyShippingNoteDTO.setContractUnitPrice(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay(new BigDecimal(0));
							hyShippingNoteDTO.setPreUnitFee(new BigDecimal(0));
							hyShippingNoteDTO.setPreFee(new BigDecimal(0));
							hyShippingNoteDTO.setCostPayShipping(new BigDecimal(0));
							hyShippingNoteDTO.setDriverFee(new BigDecimal(0));
							hyShippingNoteDTO.setCostPayTotal(new BigDecimal(0));
							hyShippingNoteDTO.setCustomerFee(new BigDecimal(0));
							hyShippingNoteDTO.setAgentCostPay(new BigDecimal(0));
							hyShippingNoteDTO.setAgentCostPayShipping(new BigDecimal(0));
							hyShippingNoteDTO.setGasRechargeAmount(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay1(new BigDecimal(0));
							hyShippingNoteDTO.setCostPay2(new BigDecimal(0));

						}
					}
				}
			}
		}

		//如果导出的运单中有货主模式的运单，需要导出货主单价
		boolean orderMode = false;
		if (null != list.stream().filter(s -> s.getOrderMode().equals("4")).findAny().orElse(null)) {
			orderMode = true;
		}
		log.info("是否是货主模式导出,{}", orderMode);

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String title = "运单表";
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		mv.addObject(NormalExcelConstants.FILE_NAME, title);
		mv.addObject(NormalExcelConstants.CLASS, HyShippingNoteDTO.class);

		//按选择列导出
		//log.info("数据-> {}", JSONUtil.toJsonStr(list));
		String selCols = request.getParameter("selCols");
		log.info("选择列导出运单-> cols {}", selCols);
		if ("1".equals(selCols)) {
			String field = request.getParameter("field");
			log.info("选择列导出运单-> field {}", field);
			if (StringUtils.isNotEmpty(field)) {
				List<String> fields = Arrays.asList(field.split(","));
				List<String> selectCols = new ArrayList<>();
				for (String s : fields) {
					if (StringUtils.isNotEmpty(s)) {
						int idx = s.indexOf("_dictText");
						if (idx > -1) {
							s = s.substring(0, idx);
						}
						selectCols.add(s);
					}
				}

				//运输路线导出，条件routeSlot
				if (fields.contains("routeSlot")) {
					selectCols.add("sendAddressName");
					selectCols.add("receiveAddressName");
				}

				//收发货区域导出,条件
				if (fields.contains("routeArea")) {
					selectCols.add("sendAreaCode");
					selectCols.add("receiveAreaCode");
				}

				if (orderMode) {
					selectCols.add("customerPrice");
					selectCols.add("customerUnitPrice");
				}

				mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(selectCols, ","));
			}
		}

		ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
		exportParams.setType(ExcelType.XSSF);
		exportParams.setStyle(HyShippingExcelExportImpl.class);
		mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		mv.addObject(NormalExcelConstants.DATA_LIST, list);
		return mv;
	}

	private void setXlyReportQueryWrapper(HyShippingNoteXlyReportDTO shippingNoteDTO, QueryWrapper<HyShippingNoteDTO> queryWrapper) {

		String shippingPayStatus = shippingNoteDTO.getShippingPayStatus();

		if (StringUtils.isNotEmpty(shippingPayStatus)) {
			if (shippingPayStatus.indexOf("!") == 0) {
				shippingPayStatus = shippingPayStatus.substring(1, shippingPayStatus.length());
				queryWrapper.ne("hy_shipping_note.shipping_pay_status", shippingPayStatus);
			} else {
				if (shippingPayStatus.contains(",")) {
					queryWrapper.in("hy_shipping_note.shipping_pay_status", Arrays.asList(shippingPayStatus.split(",")));
				} else {
					queryWrapper.eq("hy_shipping_note.shipping_pay_status", shippingPayStatus);
				}
			}
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getEtcUploadStatus())) {
			if (shippingNoteDTO.getEtcUploadStatus().contains(",")) {
				queryWrapper.in("xlyect.upload_status", Arrays.asList(shippingNoteDTO.getEtcUploadStatus().split(",")));
			} else {
				queryWrapper.eq("xlyect.upload_status", shippingNoteDTO.getEtcUploadStatus());
			}
		}else {
			queryWrapper.nested(
					qw -> qw.eq("xlyect.upload_status", "2")
							.or().eq("xlyect.upload_status","0")
							.or().isNull("xlyect.upload_status")
			);


		}

		if (shippingNoteDTO.getEtcUploadStatus()==null){
			queryWrapper.nested(
					qw -> qw.eq("shipping.ky_state", "2")
							.or().eq("shipping.ky_state","0")
							.or().isNull("shipping.ky_state")
			);
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getSendAddress())) {
			queryWrapper.like("hy_shipping_note.send_address", shippingNoteDTO.getSendAddress());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getReceiveAddress())) {
			queryWrapper.like("hy_shipping_note.receive_address", shippingNoteDTO.getReceiveAddress());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getSendAddressName())) {
			queryWrapper.like("hy_shipping_note.send_address_name", shippingNoteDTO.getSendAddressName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getReceiveAddressName())) {
			queryWrapper.like("hy_shipping_note.receive_address_name", shippingNoteDTO.getReceiveAddressName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getKhdh())) {
			queryWrapper.like("hy_shipping_note.khdh", shippingNoteDTO.getKhdh());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractNum2())) {
			queryWrapper.eq("hy_shipping_note.contract_num2", shippingNoteDTO.getContractNum2());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractStatus())) {
			queryWrapper.eq("hy_shipping_note.contract_status", shippingNoteDTO.getContractStatus());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getOrderId())) {
			queryWrapper.eq("hy_shipping_note.order_id", shippingNoteDTO.getOrderId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getCompanyId())) {
			queryWrapper.eq("hy_shipping_note.company_id", shippingNoteDTO.getCompanyId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getShippingStatus())) {
			queryWrapper.eq("hy_shipping_note.shipping_status", shippingNoteDTO.getShippingStatus());
		}

		//Added start at 2024-04-10 for 筛选是否生成运输合同
		if (StringUtils.isNotEmpty(shippingNoteDTO.getContractCreated())) {
			//0未生成，判断null和字符串空
			if ("0".equals(shippingNoteDTO.getContractCreated())) {
				queryWrapper.nested(
						qw->qw.isNull("hy_shipping_note.shipping_contract")
								.or().eq("hy_shipping_note.shipping_contract", ""));
			} else if ("1".equals(shippingNoteDTO.getContractCreated())) {
				queryWrapper.isNotNull("hy_shipping_note.shipping_contract");
				queryWrapper.ne("hy_shipping_note.shipping_contract", "");
			}

			queryWrapper.ne("hy_shipping_note.shipping_status", "8");
		}
		//Added end at 2024-04-10 for 筛选是否生成运输合同

		String orderMode = shippingNoteDTO.getOrderMode();
		if (StringUtils.isNotEmpty(orderMode)) {
			if (orderMode.contains(",")) {
				queryWrapper.in("hy_shipping_note.order_mode", Arrays.asList(orderMode.split(",")));
			} else {
				queryWrapper.eq("hy_shipping_note.order_mode", orderMode);
			}
		}
		String payChannel = shippingNoteDTO.getPayChannel();
		if (StringUtils.isNotEmpty(payChannel)) {
			if (payChannel.contains(",")) {
				queryWrapper.in("hy_shipping_note.pay_channel", Arrays.asList(payChannel.split(",")));
			} else {
				queryWrapper.eq("hy_shipping_note.pay_channel", shippingNoteDTO.getPayChannel());
			}
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getDaodaUploadStatus())) {
			queryWrapper.eq("hy_shipping_note.daoda_upload_status", shippingNoteDTO.getDaodaUploadStatus());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadNucarEtc())) {
			queryWrapper.eq("hy_shipping_note.upload_nucar_etc", shippingNoteDTO.getUploadNucarEtc());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getAlctIdentification())) {
			queryWrapper.eq("hy_shipping_note.alct_identification", shippingNoteDTO.getAlctIdentification());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTosIdentification())) {
			queryWrapper.eq("hy_shipping_note.tos_identification", shippingNoteDTO.getTosIdentification());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getUploadStatus())) {
			queryWrapper.eq("hy_shipping_note.upload_status", shippingNoteDTO.getUploadStatus());
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillStatus())) {
			queryWrapper.in("hy_shipping_note.bill_status", Arrays.asList(shippingNoteDTO.getBillStatus().split(",")));
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getDriverName())) {
			queryWrapper.like("d.driver_name", shippingNoteDTO.getDriverName());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTelephone())) {
			queryWrapper.like("d.telephone", shippingNoteDTO.getTelephone());
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getVehicleNumber())) {
			queryWrapper.like("v.vehicle_number", shippingNoteDTO.getVehicleNumber());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getShippingNoteNumber())) {
			queryWrapper.like("hy_shipping_note.shipping_note_number", shippingNoteDTO.getShippingNoteNumber());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getDescriptionOfGoods())) {
			queryWrapper.like("hy_shipping_note.description_of_goods", shippingNoteDTO.getDescriptionOfGoods());
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getCreateTime_begin())) {
			queryWrapper.ge("hy_shipping_note.create_time", shippingNoteDTO.getCreateTime_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getCreateTime_end())) {
			queryWrapper.le("hy_shipping_note.create_time", shippingNoteDTO.getCreateTime_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtReceive_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_receive", shippingNoteDTO.getStatusDtReceive_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtReceive_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_receive", shippingNoteDTO.getStatusDtReceive_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtLoading_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_loading", shippingNoteDTO.getStatusDtLoading_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtLoading_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_loading", shippingNoteDTO.getStatusDtLoading_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtSign_begin())) {
			queryWrapper.ge("hy_shipping_note.status_dt_sign", shippingNoteDTO.getStatusDtSign_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getStatusDtSign_end())) {
			queryWrapper.le("hy_shipping_note.status_dt_sign", shippingNoteDTO.getStatusDtSign_end() + " 23:59:59");
		}


		if (StringUtils.isNotEmpty(shippingNoteDTO.getTaxUploadDate_begin())) {
			queryWrapper.ge("hy_shipping_note.tax_upload_date", shippingNoteDTO.getTaxUploadDate_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getTaxUploadDate_end())) {
			queryWrapper.le("hy_shipping_note.tax_upload_date", shippingNoteDTO.getTaxUploadDate_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDt_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt", shippingNoteDTO.getPayDt_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtCheck_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt_check", shippingNoteDTO.getPayDtCheck_begin());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtCheck_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt_check", shippingNoteDTO.getPayDtCheck_end());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtSubmitAut_begin())) {
			queryWrapper.ge("hy_shipping_note.pay_dt_submit_aut", shippingNoteDTO.getPayDtSubmitAut_begin() + " 00:00:00");
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayDtSubmitAut_end())) {
			queryWrapper.le("hy_shipping_note.pay_dt_submit_aut", shippingNoteDTO.getPayDtSubmitAut_end() + " 23:59:59");
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getCarrierId())) {
			queryWrapper.eq("hy_shipping_note.carrier_id", shippingNoteDTO.getCarrierId());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getIntroducer())) {
			queryWrapper.like("hy_shipping_note.introducer", shippingNoteDTO.getIntroducer());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getDispatcher())) {
			queryWrapper.like("hy_shipping_note.dispatcher", shippingNoteDTO.getDispatcher());
		}
		//预付
		if (StringUtils.isNotEmpty(shippingNoteDTO.getPrePayStatus())) {
			queryWrapper.eq("hy_shipping_note.pre_pay_status", shippingNoteDTO.getPrePayStatus());
		}

		if (StringUtils.isNotEmpty(shippingNoteDTO.getPayType())) {
			queryWrapper.eq("hy_shipping_note.pay_type", shippingNoteDTO.getPayType());
		}

		//增加开票完成时间段查询
		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillDtComplateBegin())) {
			queryWrapper.ge("hy_shipping_note.bill_dt_complate", shippingNoteDTO.getBillDtComplateBegin());
		}
		if (StringUtils.isNotEmpty(shippingNoteDTO.getBillDtComplateEnd())) {
			queryWrapper.le("hy_shipping_note.bill_dt_complate", shippingNoteDTO.getBillDtComplateEnd());
		}

	}
}
